Java Reference
In-Depth Information
that all the elements be rehashed and stored in the correct new bucketa
costly operation.
You need to consider the load factor and the initial capacity when cre-
ating 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 nu-
merous 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 it-
eration 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 order the 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 over-
head of maintaining the linked list structure; however, iteration only re-
quires 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
putAll however, 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.
 
Search WWH ::




Custom Search