Maps – Collections: Part II
By Jaime Williams / April 23, 2021 / No Comments / Composing Stream Pipelines, Executing Stream Pipelines, Introduction to Streams, Oracle Certification Exam, Replacing Elements in Collections, The NavigableSet Interface
15.8 Maps
A map defines mappings from keys to values. The <key, value> pair is called a mapping, also referred to as an entry. A map does not allow duplicate keys; in other words, the keys are unique. Each key maps to one value at most, implementing what is called a single-valued map. Thus there is a many-to-one relationship between keys and values. For example, in a student-grade map, many students (keys) can be awarded the same grade (value), but each student has only one grade. Replacing the value that is associated with a key results in the old entry being removed and a new entry being inserted.
Both the keys and the values must be objects, with primitive values being wrapped in their respective primitive wrapper objects when they are put in a map.
The Map<K,V> Interface
A map is not a collection, and the Map<K, V> interface does not extend the Collection<E> interface. However, the mappings can be viewed as a collection in various ways: a key set, a value collection, or an entry set. A key set view or an entry set view can be iterated over to retrieve the corresponding values from the underlying map (p. 844).
The Map<K, V> interface specifies some optional methods. Implementations should throw an UnsupportedOperationException if they do not support such an operation. The implementations of maps from the java.util package support all the optional operations of the Map<K, V> interface (see Table 15.2, p. 788, and Figure 15.3, p. 787).
The Map<K,V> interface and its subinterfaces SortedMap<K,V> and NavigableMap<K,V> provide a versatile set of methods to implement a wide range of operations on maps. Several examples in this section and subsequent sections illustrate many of the methods specified in these interfaces.
Basic Key-Based Operations
These operations constitute the basic functionality provided by a map. Many of the methods will be used in examples presented in this chapter.
V put(K key, V value)
Optional
default V putIfAbsent(K key, V value)
The put() method inserts the <key, value> entry into the map. It returns the old value previously associated with the specified key, if any. Otherwise, it returns the null value.
The default method putIfAbsent() associates the key with the given value and returns null if the specified key is not already associated with a non-null value; otherwise, it returns the currently associated value.
V get(Object key)
default V getOrDefault(Object key, V defaultValue)
The get() method returns the value to which the specified key is mapped, or null if no entry is found.
The default method getOrDefault() returns the value to which the specified key is mapped, or the specified defaultValue if this map contains no entry for the key.
V remove(Object key)
Optional
default boolean remove(Object key, Object value)
The remove() method deletes the entry for the specified key. It returns the value previously associated with the specified key, if any. Otherwise, it returns the null value.
The default method remove() removes the entry for the specified key and returns true only if the specified key is currently mapped to the specified value.
default V replace(K key, V value)
default boolean replace(K key, V oldValue, V newValue)
In the first replace() method, the value associated with the key is replaced with the specified value only if the key is already mapped to some value. It returns the previous value associated with the specified key, or null if there was no entry found for the key.
In the second replace() method, the value associated with the key is replaced with the specified newValue only if the key is currently mapped to the specified oldValue. It returns true if the value in the entry for the key was replaced with the newValue.
boolean containsKey(Object key)
boolean containsValue(Object value)
The containsKey() method returns true if the specified key is mapped to a value in the map.
The containsValue() method returns true if there exists one or more keys that are mapped to the specified value.