THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

The expected maximum size is a hint for the initial capacity of the map, but its exact effects are not specified.


21.8.4. WeakHashMap


The collection implementations all use strong references for elements, values, and keys. As with strong
references elsewhere, this is usually what you want. Just as you occasionally need reference objects to provide
weaker references, you also occasionally need a collection that holds the objects it contains less strongly. You
can use WeakHashMap in such cases.


WeakHashMap behaves like HashMap but with one differenceWeakHashMap refers to keys by using
WeakReference objects instead of strong references. As you learned in Section 17.5 on page 454, weak
references let the objects be collected as garbage, so you can put an object in a WeakHashMap without the
map's reference forcing the object to stay in memory. When a key is only weakly reachable, its mapping may
be removed from the map, which also drops the map's strong reference to the key's value object. If the value
object is otherwise not strongly reachable, this could result in the value also being collected.


A WeakHashMap checks to find unreferenced keys when you invoke a method that can modify the contents
(put, remove, or clear), but not before get. This makes the performance of such methods dependent on
the number of keys that have become unreferenced since the last check. If you want to force removal you can
invoke one of the modifying methods in a way that will have no effect, such as by removing a key for which
there is no mapping (null, for example, if you do not use null as a key). Because entries in the map can
disappear at any time, the behavior of some methods can be surprisingfor example, successive calls to size
can return smaller and smaller values; or an iterator for one of the set views can throw
NoSuchElementException after hasNext returns true.


The WeakHashMap class exports the same constructors as HashMap: a no-arg constructor; a constructor
that takes an initial capacity; a constructor that takes an initial capacity and a load factor; and a constructor
that takes a Map whose contents will be the initial contents of the WeakHashMap.


Exercise 21.2: Rewrite the DataHandler class on page 457 to use a WeakHashMap to store the returned
data instead of a single WeakReference.


Exercise 21.3: A WeakHashMap has weak keys and strong values. A WeakValueMap would have strong
keys and weak values. Design a WeakValueMap. Be cautioned that this is not as simple as it might seem, in
fact it is extremely complicated and requires a number of design choices to be made. For example, should
iteration of values be allowed to yield null after hasNext has returned TRue, or should iteration keep the
values alive while they are being iterated? Hint: Don't try to extend AbstractMap, delegate to a HashMap
instead.


21.8.5. treeMap


The TReeMap class implements SortedMap, keeping its keys sorted in the same way as TReeSet. This
makes adding, removing, or finding a key/value pair 0(logn). So you generally use a treeMap only if you
need the sorting or if the hashCode method of your keys is poorly written, thereby destroying the 0(1)
behavior of HashMap.


treeMap has the following constructors:


publictreeMap()

Creates a new treeMap that is sorted according to the natural order of the
keys. All elements added to this map must implement the Comparable
Free download pdf