bucketa costly operation.
You need to consider the load factor and the initial capacity when creating the hash map, be aware of the
expected number of elements the map will hold, and be aware of the expected usage patterns of the map. If the
initial capacity is too small and the load factor too low, then numerous rehash operations will occur. In
contrast, with a large enough initial capacity and load factor, a rehash might never be needed. You need to
balance the cost of normal operations against the costs of iteration and rehashing. The default load factor of
0.75 provides a good general trade-off.
21.8.2. LinkedHashMap
LinkedHashMap<K,V> extends HashMap<K,V> and refines the contract of HashMap by defining an
order to the entries in the map. Iterating through the contents of a LinkedHashMap (either the entry set or
the key set) will, by default, return the entries in insertion orderthe order in which they were added. Aside
from this, LinkedHashMap behaves like HashMap and defines constructors of the same forms. The
performance of a LinkedHashMap is likely to be a little slower than a HashMap due to the overhead of
maintaining the linked list structure; however, iteration only requires a time proportional to the size,
regardless of capacity.
Additionally, LinkedHashMap provides a constructor that takes the initial capacity, the load factor and a
boolean flag accessOrder. If accessOrder is false, then the map behaves as previously described
with respect to ordering. If accessOrder is true, then the map is sorted from the most recently accessed
entry to the least recently accessed entry, making it suitable for implementing a Least Recently Used (LRU)
cache. The only methods to count as an access of an entry are direct use of put, get, and putAllhowever,
if invoked on a different view of the map (see Section 21.10 on page 597), even these methods do not count as
an access.
21.8.3. IdentityHashMap
The general contract of Map requires that equality for keys be based on equivalence (that is, using the
equals method). But there are times when you really want a map that stores information about specific
objects instead of treating all equivalent objects as a single key to information. Consider the serialization
mechanism discussed in Chapter 20. When determining if an object in the serialization graph has already been
seen, you need to check that it is the actual object, not just one that is equivalent. To support such
requirements the IdentityHashMap class uses object identity (comparison using ==) to determine if a
given key is already present in the map. This is useful, but it does break the general contract of Map.
There are three constructors:
publicIdentityHashMap(int expectedSize)
Creates a new IdentityHashMap with the given expected maximum size.
publicIdentityHashMap()
Creates a new IdentityHashMap with the default expected maximum
size.
publicIdentityHashMap(Map<? extends K,? extends V> map)
Creates a new IdentityHashMap containing the entries from the given
map.