Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashing Keys in Java

In java, when I use a String as a key for Hashmap I get a little different result than when I use the string hashcode as a key in the HashMap.

Any insight?

like image 978
user1785771 Avatar asked Nov 03 '12 10:11

user1785771


People also ask

How do you generate a hash key in Java?

MD5 Message Digest Algorithm Here we have used the digest() method of the MessageDigest class from the java. security package to create the MD5 hash in bytes and then converted those bytes to hex format to generate the hash as text.

What is a hashing key?

The hashing key is the raw data in which to be hashed. The hashing algorithm is the algorithm which performs a function to convert the hash key to the hash value. the hash value is what is produced as a result of the hash key being passed into the hashing algorithm.

Are there hashes in Java?

Java provides multiple ways to implement hashing. Some of the most popular ways are using the HashMap and HashSet classes. Both the HashMap and HashSet classes use hashing algorithms to store and retrieve data.

How do hashes work in Java?

In hashing there is a hash function that maps keys to some values. But these hashing function may lead to collision that is two or more keys are mapped to same value. Chain hashing avoids collision. The idea is to make each cell of hash table point to a linked list of records that have same hash function value.


1 Answers

when I use the string hashcode as a key in the HashMap.

You mustn't use the hash code itself as the key. Hash codes aren't intended to be unique - it's entirely permitted for two non-equal values to have the same hash code. You should use the string itself as a key. The map will then compare hash codes first (to narrow down the candidate matches quickly) and then compare with equals for genuine string equality.

Of course, that's assuming your code really is as your question makes it, e.g.

HashMap<String, String> goodMap = new HashMap<String, String>();
goodMap.put("foo", "bar");

HashMap<Integer, String> badMap = new HashMap<Integer, String>();
badMap.put("foo".hashCode(), "bar");

If that's really what your code looks like, just use HashMap<String, String> instead.

From the docs for Object.hashCode() (emphasis mine):

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
like image 82
Jon Skeet Avatar answered Sep 24 '22 00:09

Jon Skeet