I wrote a hashmap and filled it with key/value entries. The values are objects. The map contains at least 10 thousand entries. Later on when I want to fetch the values using a certain key, the map seems to not find this entry. This is too weird. Even each time it is failing at a different entry. What can I do to troubleshoot this? I am using weblogic as server. Should I play with any environment value?
worst case occured when all the stored object are in the same index in the hashtable. so they will be stored in a linkedlist in which we (may) need to walk through all of them to find our searched object. hope this help,.
By definition a HashMap in Java stores all keys uniquely. Simply, when we try to put more entries with the same key it will override the previously defined value for that key. Hence, when deleting a key after putting multiple values for that key means the key will no longer exist in the HashMap .
A collision, or more specifically, a hash code collision in a HashMap, is a situation where two or more key objects produce the same final hash value and hence point to the same bucket location or array index.
Disadvantages of HashMapPotential of collision when 2 distinct keys generate the same hashCode() value worse the performance of the hashMap. Occasionally HashMaprequires resizing when the original size of HashMap buckets are full.
Here are some things that may cause entries to be "lost":
Incorrectly implemented equals / hashcode methods on your key objects. These two methods need to conform to a "contract" for the hash table to work properly. The most important property is:
key1.equals(key2) IMPLIES key1.hashcode() == key2.hashcode
Mutable key objects that are changed while the key is used in the map. In particular, if the key change cause the key's hashcode to change, it will be lost. (In this case, the entries will show up if you iterate over the entire map. But the map operations that use has lookup won't find them ... because they are on the wrong hash chain.)
The safest approach is to use a key class that is immutable.
Use of a map in a multi-threaded application without proper synchronization. This can cause corruption of the map data structure which could manifest as lost entries.
Some other part of your application that you are not aware of is removing the entries.
The answers that state/stated that you have to override equals
and hashcode
are/were incorrect. There are situations where the Object
implementations of these methods are exactly what is required. What you have to do is make sure that:
equals
and hashcode
conform to "the contract".What can I do to troubleshoot this?
I'd recommend a code inspection to check the above issues.
Debugging is another alternative. For instance, you could look to see if you can find the missing entries on the wrong hash chain. However, I suspect that approach could be a bit "hit and miss".
I am using weblogic as server.
Not relevant ... unless you happen to be using some Map implementation class that is implemented by Weblogic rather than J2SE. (Look at the object's classname.)
Should I play with any environment value?
No. It won't help
One possible scenario is:
If key is instance of class and that class didn't override equals/hashcode methods, then get method may return false.
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