Name

Map<K,V>

Synopsis

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, ...

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.