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?
Share
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:
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
equalsandhashcodeare/were incorrect. There are situations where theObjectimplementations of these methods are exactly what is required. What you have to do is make sure that:equalsandhashcodeconform to "the contract".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".
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.)
No. It won’t help