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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With