O'Reilly logo

Java Generics and Collections by Philip Wadler, Maurice Naftalin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 16. Maps

The Map interface is the last of the major Collections Framework interfaces, and the only one that does not inherit from Collection. It defines the operations that are supported by a set of key-to-value associations in which the keys are unique. These operations are shown in Figure 16-1 and fall into the following four groups, broadly parallel to the four operation groups of Collection—adding elements, removing elements, querying collection contents, and providing different views of the contents of a collection.

Adding Associations

V put(K key, V value)            // add or replace a key-value association
                                 // return the old value (may be null) if the
                                 // key was present; otherwise returns null
void putAll(Map<? extends K,? extends V> m)
                                 // add each of the key-value associations in
                                 // the supplied map into the receiver
Map
Figure 16-1. Map

The operations in this group are optional; calling them on an unmodifiable map will result in an UnsupportedOperationException.

Removing Associations

void clear()               // remove all associations from this map
V remove(Object key)       // remove the association, if any, with the
                           // given key; returns the value with which it
                           // was associated, or null

The signature of Map.remove is like that of the Collection.remove (see Using the Methods of Collection) in that it takes a parameter of type Object rather than the generic type. We discussed alternatives to this design ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required