Mapimplementations are grouped into general-purpose, special-purpose, and concurrent implementations.General-Purpose Map Implementations
The three general-purposeMapimplementations areHashMap,TreeMapandLinkedHashMap. If you needSortedMapoperations or key-orderedCollection-view iteration, useTreeMap; if you want maximum speed and don't care about iteration order, useHashMap; if you want near-HashMapperformance and insertion-order iteration, useLinkedHashMap. In this respect, the situation forMapis analogous toSet. Likewise, everything else in the Set Implementations section also applies toMapimplementations.
LinkedHashMapprovides two capabilities that are not available withLinkedHashSet. When you create aLinkedHashMap, you can order it based on key access rather than insertion. In other words, merely looking up the value associated with a key brings that key to the end of the map. Also,LinkedHashMapprovides theremoveEldestEntrymethod, which may be overridden to impose a policy for removing stale mappings automatically when new mappings are added to the map. This makes it very easy to implement a custom cache.For example, this override will allow the map to grow up to as many as 100 entries and then it will delete the eldest entry each time a new entry is added, maintaining a steady state of 100 entries.
private static final int MAX_ENTRIES = 100; protected boolean removeEldestEntry(Map.Entry eldest) { return size() > MAX_ENTRIES; }Special-Purpose Map Implementations
There are three special-purpose Map implementations EnumMap,WeakHashMapandIdentityHashMap.EnumMap, which is internally implemented as anarray, is a high-performanceMapimplementation for use with enum keys. This implementation combines the richness and safety of theMapinterface with a speed approaching that of an array. If you want to map an enum to a value, you should always use anEnumMapin preference to an array.
WeakHashMapis an implementation of theMapinterface that stores only weak references to its keys. Storing only weak references allows a key-value pair to be garbage-collected when its key is no longer referenced outside of theWeakHashMap. This class provides the easiest way to harness the power of weak references. It is useful for implementing "registry-like" data structures, where the utility of an entry vanishes when its key is no longer reachable by any thread.
IdentityHashMapis an identity-basedMapimplementation based on a hash table. This class is useful for topology-preserving object graph transformations, such as serialization or deep-copying. To perform such transformations, you need to maintain an identity-based "node table" that keeps track of which objects have already been seen. Identity-based maps are also used to maintain object-to-meta-information mappings in dynamic debuggers and similar systems. Finally, identity-based maps are useful in thwarting "spoof attacks" that are a result of intentionally perverseequalsmethods becauseIdentityHashMapnever invokes theequalsmethod on its keys. An added benefit of this implementation is that it is fast.Concurrent Map Implementations
Thejava.util.concurrentpackage contains theConcurrentMapinterface, which extendsMapwith atomicputIfAbsent,remove, andreplacemethods, and theConcurrentHashMapimplementation of that interface.
ConcurrentHashMapis a highly concurrent, high-performance implementation backed up by a hash table. This implementation never blocks when performing retrievals and allows the client to select the concurrency level for updates. It is intended as a drop-in replacement forHashtable: in addition to implementingConcurrentMap, it supports all the legacy methods peculiar toHashtable. Again, if you don't need the legacy operations, be careful to manipulate it with theConcurrentMapinterface.