Java Fastest Hash Function
-
02-06-2021 - |
Вопрос
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 ?
Решение
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
.
Другие советы
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.
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.