Modern Java in Action 3 - collection API
book notes: collection API enhancements

New API (in Java 8)
Collection classes got a few nice additions.
Factory methods
They create immutable collection (if you try to add/remove elements, you get UnsupportedOperationException. There is no varargs variant - this variant would require additional array allocation, which non-varargs variants don’t have.
There are overloads from 1 to 10 elements.
|
|
lists: removeIf, replaceAll
On a list you can use removeIf and replaceAll
|
|
maps: forEach, comparingBy{Key,Value}
On a map you can iterate over pairs without the need to use an iterator that gives Map.Entry<K, V>:
|
|
and you can sort by keys or values:
|
|
HashMap performance
If keys are Comparable, HashMap implementation uses sorted trees with O(log(n)) retrieval instead of LinkedList which have O(n) retrieval complexity (those data structures are used for keeping entries with the same key hashcode).
getOrDefault, compute{,IfAbsent,IfPresent}
Other methods worth mentioning allow:
V getOrDefault(Object key, V defaultValue)
: get value by key but provide a defualt value in case there is no such keyV compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
- attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
- if the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless nullV computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
- if the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value
replace, replaceAll, merge
- replace - replace a value in the Map if a key is present
- replaceAll - replaces each entry’s value with the result of applying a BiFunction
- merge - takes a BiFunction to merge values that have a duplicate key
ConcurrentHashMap
Concurrency-friendly map with new methods that work with X where X means: keys, values, entries or pairs of key and value:
- forEach{,Key,Value,Entry} - Performs a given action for each X
- reduce{,Key,Value,Entry} - Combines all X-es given a reduction function into a result
- search{,Key,Value,Entry} - Applies a function on each X until the function produces a non-null result
- reduce{,Keys,Values,Entrys}{,ToInt, ToLong,ToDouble} - reduction, possibly to primitive types
These functions require providing parallelism threashold:
- value of 1: maximal parallelism using the common thread pool
- value of Long.MAX_VALUE: runs the operation on a single thread
Also:
- mappingCount - returns long, not int; preferrable over
size
- keySet - creates a view of keys backed by original map
Ten wpis jest częścią serii modern-java-in-action.
- 2022-05-07 - Modern Java in Action 8 - concurrency and reactive programming
- 2022-02-07 - Modern Java in Action 7 - notes about the module system
- 2022-30-06 - Modern Java in Action 6 - Time and Date
- 2022-20-06 - Modern Java in Action 5 - Optional
- 2022-17-06 - Modern Java in Action 4 - refactoring and testing
- 2022-15-06 - Modern Java in Action 3 - collection API
- 2022-14-06 - Modern Java in Action 2 - fork-join and spliterators
- 2022-03-06 - Modern Java in Action 1 - Java 8 refresher