The hashCode and equals functions are used to find a key.Given a key value the time taken to compute the hashCode would be the same for any key value.
In case of add, it computes the hashCode of the key to be added and lands up in a say bucket, does equals comparision with the keys in the bucket , replaces oldkey's value with newkey's value the same key is found and adds key if no key is found..
In case of contains it computes the hashCode of the key to be searched and lands up in a bucket, does equals comparision with the keys in the bucket and the input key and returns true or false.
In case of remove too, it computes the hashCode of the key to be searched and lands up in a bucket, does equals comparision with the keys in the bucket and the input key and removes the key value if found.
So all in all the hash computing time is constant, landing up in a bucket time is constant, the doing equals comparision within a bucket might vary depending on how efficient the hash algorithm is but generally the time for all this operations can be considered constant.
first feature (key/value pairs) The only collections that provide keys are maps. Maps are collections that have "map" in their name. Hashtable is also a map, but has no "map" in its name because of historical reasons. So we can strike out all wrong answers and only LinkedHashMap TreeMap HashMap Hashtable (old synchronized version of HashMap) remain.
second feature (Duplicate entries replace old) true for all maps, cause duplicate keys "override" old key value pairs.
third feature (constant-time performance for add...) TreeMap is ordered. Every time you add something, the tree has to be ordered again. And the time to do this depends if you add in the middle or at the tail. IE it depends if you add an "N" or a "Z", simply speaking. Re-Ordering also takes place when you remove(), so we can strike TreeMap out two times. LinkedHashMap HashMap Hashtable remain.
About LinkedHashMap: It is not ordered, just sorted (by entry time). Every time you add a pair it is just glued to the tail, should be time constant.
all events occur in real time
Joined: Feb 28, 2007
Thanks a lot Burkhard,
Ramification of answering was great. I got it step by step as you described. It can help me to find answers of the similar questions.