I have a Boolean string (like "01100..001") of length 128 characters (means 128 number of 0/1). I am searching for an efficient (fast) hash function in Java, which produce a much lower representation than 128 bit and obviously with less collision. Can anybody help me, is there any such hash function ? Any suggestion ?
-
3Less collision than the zero you'd get with a 128-bit representation?eggyal– eggyal2012-04-22 17:15:05 +00:00Commented Apr 22, 2012 at 17:15
-
@eggyal, Thanks a lot. Nice concept. It will help me lot. :)Arpssss– Arpssss2012-04-22 17:27:28 +00:00Commented Apr 22, 2012 at 17:27
-
Using a String for just storing a 128-bit value seems to me a bit of overkill, a waste of memory and - especially if you care on performance - definitely not the best option.MRalwasser– MRalwasser2012-04-22 17:41:49 +00:00Commented Apr 22, 2012 at 17:41
3 Answers
Have you considered using a java.util.BitSet instead, depending on what you are doing it could be a lot easier and more efficient? http://docs.oracle.com/javase/6/docs/api/java/util/BitSet.html
It has a .hashCode() method as well.
2 Comments
Try using the .hashCode() method on Java String class, it returns an int and it is very fast.
Or you can use the .hashCode() method on java.util.BitSet as Pulsar suggest, if you prefer store your data in BitSet.
5 Comments
String to BigInteger first and called the .hashCode() method for that. But I'm guessing it's faster to just hash the original String like you suggested. Just wondering why you'd want to have 16 bytes stored as a 128 byte String, that seems like a huge waste of space.hashCode() of BitSet as per @Pulsar's answer is probably better.If you need to calculate the hash of a string, simply use the hashCode() method of the String class. Depending on the implementation, several optimizations are made for quickly computing this value.
As an example, in OpenJDK's implementation of the String class the hashCode() method caches the value in the hash attribute and only needs to be computed once.
And who said that a string of 128 characters has a hash of 128-bits? all hashes returned by the hashCode() method in Java are of type int, and ints in Java are represented using 32-bits.