Unpacking this definition:
Summarizing the last two points: "equal yield equal always; unequal yield unequal often." This is the most important characteristic of all hash functions.
A high-quality hash function strives for some subset of the following virtues:
The primary way to provide the data that your hash function should act on is via a {@link Hasher}. Obtain a new hasher from the hash function using {@link #newHasher}, "push" the relevant data into it using methods like {@link Hasher#putBytes(byte[])}, and finally ask for the {@code HashCode} when finished using {@link Hasher#hash}. (See an {@linkplain #newHasher example} of this.)
If all you want to hash is a single byte array, string or {@code long} value, thereare convenient shortcut methods defined directly on {@link HashFunction} to make thiseasier.
Hasher accepts primitive data types, but can also accept any Object of type {@code T} provided that you implement a {@link Funnel Funnel Compatibility note: Throughout this API, multibyte values are always interpreted in little-endian order. That is, hashing the byte array {@code}{0x01, 0x02, 0x03, 0x04}} is equivalent to hashing the {@code int} value {@code 0x04030201}. If this isn't what you need, methods such as {@link Integer#reverseBytes}and {@link Ints#toByteArray} will help. Java's baked-in concept of hash codes is constrained to 32 bits, and provides no separation between hash algorithms and the data they act on, so alternate hash algorithms can't be easily substituted. Also, implementations of {@code hashCode} tendto be poor-quality, in part because they end up depending on other existing poor-quality {@code hashCode} implementations, including those in many JDK classes. {@code Object.hashCode} implementations tend to be very fast, but have weakcollision prevention and no expectation of bit dispersion. This leaves them perfectly suitable for use in hash tables, because extra collisions cause only a slight performance hit, while poor bit dispersion is easily corrected using a secondary hash function (which all reasonable hash table implementations in Java use). For the many uses of hash functions beyond data structures, however, {@code Object.hashCode} almostalways falls short -- hence this library.
@author Kevin Bourrillion
@since 11.0
Relationship to {@link Object#hashCode}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|