Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Map
implementations are grouped into general-purpose, special-purpose, and concurrent implementations.General-Purpose Map Implementations
The three general-purposeMap
implementations areHashMap
,TreeMap
andLinkedHashMap
. If you needSortedMap
operations or key-orderedCollection
-view iteration, useTreeMap
; if you want maximum speed and don't care about iteration order, useHashMap
; if you want near-HashMap
performance and insertion-order iteration, useLinkedHashMap
. In this respect, the situation forMap
is analogous toSet
. Likewise, everything else in the Set Implementations section also applies toMap
implementations.
LinkedHashMap
provides two capabilities that are not present onLinkedHashSet
. 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,LinkedHashMap
provides theremoveEldestEntry
method, 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
,WeakHashMap
andIdentityHashMap
.EnumMap
, which is internally implemented as anarray
, is a high-performanceMap
implementation for use with enum keys. This implementation combines the richness and safety of theMap
interface with a speed approaching that of an array. If you want to map an enum to a value, you should always use anEnumMap
in preference to an array.
WeakHashMap
is an implementation of theMap
interface 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.
IdentityHashMap
is an identity-basedMap
implementation 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 perverseequals
methods becauseIdentityHashMap
never invokes theequals
method on its keys. An added benefit of this implementation is that it is fast.Concurrent Map Implementations
Thejava.util.concurrent
package contains theConcurrentHashMap
class, which extendsMap
with atomicputIfAbsent
,remove
, andreplace
methods, and theConcurrentHashMap
implementation of that interface.
ConcurrentHashMap
is 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 theConcurrentMap
interface.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.