Name

AbstractMap<K,V>

Synopsis

This abstract class is a partial implementation of the Map interface that makes it easy to define simple custom Map implementations. To define an unmodifiable map, subclass AbstractMap and override the entrySet( ) method so that it returns a set of Map.Entry objects. (Note that you must also implement Map.Entry, of course.) The returned set should not support add( ) or remove( ), and its iterator should not support remove( ). In order to define a modifiable Map, you must additionally override the put( ) method and provide support for the remove( ) method of the iterator returned by entrySet( ).iterator( ). In addition, it is conventional that all Map implementations define two constructors: one that accepts no arguments and another that accepts a Map of initial mappings.

AbstractMap defines all Map methods in terms of its entrySet( ) and put( ) methods and the remove( ) method of the entry set iterator. Note, however, that the implementation is based on a linear search of the Set returned by entrySet( ) and is not efficient when the Map contains more than a handful of entries. Some subclasses may want to override additional AbstractMap methods to improve performance. HashMap and TreeMap use different algorithms are are substantially more efficient.

java.util.AbstractMap<K,V>

Figure 16-3. java.util.AbstractMap<K,V>

public abstract class AbstractMap<K,V> implements Map<K,V> {  ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.