I am preparing for software interviews and i am stuck with a question for days now.
I have not been able to figure out the difference between linkedhashmap, map, hashtable, hashmap present in the Java Collection API.
Do all of these have the same get and put complexities? I know that map is the interface class
and hashmap, hashtable, linkedhashmap implement this interface. So does that mean that the inner implementation of these 3 classes is the same? How are they implemented in the collections api?
Thanks in Advance!!!
I doubt the differences can be explained significantly better than what’s already written in the JavaDocs for these classes:
ConcurrentHashMapandConcurrentSkipListMap.All of the aforementioned
Mapimplementations have their basic get/put operations in (amortized) O(1) time complexity. There are minor differences in the handling ofnullvalues, it’s inevitable to check the JavaDoc for details.To get an idea of how these classes are implemeted, have a look at their inheritance tree:
Map(just the interface)Dictionary(obsoleted abstract class)Hashtable(the “old” map implementation lives on it’s own)AbstractMap(the basic functionality of the “new” map implementations)HashMap(the first concrete map implementation for general purpose use)LinkedHashMap(extendsHashMapby mainaining the linked list)