Bulk Operations – Collections: Part II
By Jaime Williams / August 23, 2023 / No Comments / Composing Stream Pipelines, Introduction to Streams, Oracle Certification Exam
Bulk Operations
Bulk operations can be performed on an entire map.
int size()
boolean isEmpty()
Return the number of entries (i.e., number of unique keys in the map) and whether the map is empty or not, respectively.
void clear()
Optional
void putAll(Map<? extends K, ? extends V> map)
Optional
The first method deletes all entries from the current map.
The second method copies all entries from the specified map to the current map. If a key from the specified map is already in the current map, its associated value in the current map is replaced with the associated value from the specified map.
default void forEach(BiConsumer<? super K,? super V> action)
The specified action is performed for each entry in this map until all entries have been processed or the action throws an exception. Iteration is usually performed in entry set iteration order. The functional interface BiConsumer<T,U> is covered in §13.7, p. 711, where this method is illustrated. It is also used in Example 15.10, p. 844.
default void replaceAll(BiFunction<? super K,? super V,? extends V> func)
The value of each entry is replaced with the result of invoking the given twoarity function on the entry until all entries have been processed or the function throws an exception. The functional interface BiFunction<T,U,R> is covered in §13.9, p. 717, where this method is illustrated.
Collection Views
Views allow information in a map to be represented as collections. Elements can be removed from a map via a view, but cannot be added. An iterator over a view will throw an exception if the underlying map is modified concurrently. Example 15.10, p. 844, illustrates collection views.
Set<K> keySet()
Collection<V> values()
Set<Map.Entry<K, V>> entrySet()
Create different views of a map. Changes in the map are reflected in the view, and vice versa. These methods return a set view of keys, a collection view of values, and a set view of <key, value> entries, respectively. Note that the Collection returned by the values() method is not a Set, as several keys can map to the same value—that is, duplicate values can be included in the returned collection. An entry in the entry set view can be manipulated by the methods defined in the Map.Entry<K, V> interface.
Example 15.10, p. 844, provides an example of using collection views.