This interface
represents a collection of mappings, or associations, between key
objects and value objects. Hashtables and associative arrays are
examples of maps. In Java 5.0 this interface has been made generic.
The type variable K represents the type of
the keys held by the map and the type variable
V represents the type of the values
associated with those keys.
The set of key objects in a Map must not have any
duplicates; the collection of value objects is under no such
constraint. The key objects should usually be immutable objects, or,
if they are not, care should be taken that they do not change while
in use in a Map. As of Java 1.2, the
Map interface replaces the abstract
Dictionary class. Although a
Map is not a Collection, the
Map interface is still considered an integral
part, along with Set, List, and
others, of the Java collections framework.
You can
add a key/value association to a Map with the
put( ) method. Use putAll( ) to
copy all mappings from one Map to another. Call
get( ) to look up the value object associated with
a specified key object. Use remove( ) to delete
the mapping between a specified key and its value, or use
clear( ) to delete all mappings from a
Map. size( ) returns the number
of mappings in a Map, and isEmpty(
) tests whether the Map contains no
mappings. containsKey( ) tests whether a
Map contains the specified key object, and
containsValue( ) tests whether it contains the
specified value. (For most implementations, containsValue(
) is a much more expensive operation than
containsKey( ), however.) keySet(
) returns a Set of all key objects in
the Map. values( ) returns a
Collection (not a Set, since it
may contain duplicates) of all value objects in the map.
entrySet( ) returns a Set of
all mappings in a Map. The elements of this
returned Set are Map.Entry
objects. The collections returned by values( ),
keySet( ), and entrySet( ) are
based on the Map itself, so changes to the
Map are reflected in the collections.
An interface cannot specify constructors, but it is conventional that
all implementations of Map provide at least two
standard constructors: one that takes no arguments and creates an
empty map, and a copy constructor that accepts a
Map object that specifies the initial contents of
the new Map.
Implementations are required to support all methods that query the
contents of a Map, but support for methods that
modify the contents of a Map is optional. If an
implementation does not support a particular method, the
implementation of that method simply throws a
java.lang.UnsupportedOperationException. See also
Collection, Set,
List, HashMap,
Hashtable, WeakHashMap,
SortedMap, and treeMap.
public interface Map<K,V> {
// Nested Types
public interface Entry<K,V>;
// Public Instance Methods
void clear( );
boolean containsKey(Object key);
boolean containsValue(Object value);
Set<Map.Entry<K,V>> entrySet( );
boolean equals(Object o);
V get(Object key);
int hashCode( );
boolean isEmpty( );
Set<K> keySet( );
V put(K key, V value);
void putAll(Map<? extends K,? extends V> t);
V remove(Object key);
int size( );
Collection<V> values( );
}
Too many methods to list.
Too many methods to list.