Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 683
نمايش فراداده

AbstractMap<K,V>java.util

Java 1.2collection

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.

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

public abstract class

AbstractMap<K,V> implements Map<K,V> { // Protected Constructors protected

AbstractMap ( ); // Methods Implementing Map public void

clear ( ); public boolean

containsKey (Object

key ); public boolean

containsValue (Object

value ); public abstract Set<Map.Entry<K,V>>

entrySet ( ); public boolean

equals (Object

o ); public V

get (Object

key ); public int

hashCode ( ); public boolean

isEmpty ( ); public Set<K>

keySet ( ); public V

put (K

key , V

value ); public void

putAll (Map<? extends K,? extends V>

t ); public V

remove (Object

key ); public int

size ( ); public Collection<V>

values ( ); // Public Methods Overriding Object public String

toString ( ); // Protected Methods Overriding Object

1.4 protected Object

clone ( ) throws CloneNotSupportedException; }

Subclasses

EnumMap, HashMap, IdentityHashMap, TReeMap, WeakHashMap, java.util.concurrent.ConcurrentHashMap