Java 8 – New methods in Collections


In the first article we learned about Lambdas, functional interfaces and method references introduced in Java 8. In the previous article we saw default methods in interfaces and their inheritance rules. In this article we look at the new default methods added in the Collections hierarchy. Many of the new methods make use of lambda expressions to simplify operations on Collections.These methods make iterating through the collections easier. The developer is freed from actually performing the iteration, and can concentrate only on what happens in each iteration. The advantage is a) easier to read code and b) faster to develop. Here are the methods

void forEach(Consumer<? super T> action) Iterable

Iterates over each element of the List and calls the lambda expression specified by ‘action’.

boolean removeIf(Predicate<? super E> filter) Collection

Iterates through the Collection and removes the element that matches the filter.

boolean removeIf(Predicate<? super E> filter)Collection

This is a very useful method. It replaces all elements in the List with the result of applying the operator (apply method)

void sort(Comparator<? super E> c)List

Sorts the element using the provided comparator. This example sorts the elements in descending order

void forEach(BiConsumer<? super K, ? super V> action)Map

This method performs the operation specified in the ‘action’ on each Map Entry (key and value pair). It iterates in the order of the key set.

V compute(K key,BiFunction<? super K, ? super V, ? extends V> remappingFunction)List

This method replaces the value of a key by the value computer from the remappingFunction. In this example we replace the number of books written by Clive Cussler to original count(50) +1If the compute function returns null then the entry for that key is removed from the map. If the key is not present then a new entry is added.

V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)Map

This method adds an entry in the Map. the key is specified in the function and the value is the result of the application of the mapping function. In our slightly weird example, we add the number of books written by ‘Agatha Christie’ as the number of alphabets in the authors nameThe entry is added only if the computed value is not null.

V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunctionMap

This function is simlar to the compute function, however the difference is that the compute function adds or modifies an existing entry whereas this function does nothing if an entry with that key is not present. Note that this function also removes an element if the new value computed from the passed lambda expression is null.

V getOrDefault(Object key, V defaultValue)Map

Returns the value mapped to the key, or if the key is not present, returns the default value.the map does not contain ‘AuthorA’ so this returns 0.

V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunctionMap

If the key is not present or if the value for the key is null, then adds the key-value pair to the map. If the key is present then replaces the value with the value from the remapping function. If the remapping function return null then the key is removed from the map.

V putIfAbsent(K key, V value)Map

if the key is not present or if the key is mapped to null, then the key-value pair is added to the map and the result returns null. If the key is already mapped to a value, then that value is returned

boolean remove(Object key, Object value) Map

removes the key only if its associated with the given value

V replace(K key, V newValue) Map

If the key is present then the value is replaced by newValue. If the key is not present, does nothing.

boolean replace(K key, V oldValue, V newValue) Map

If the key is present and is mapped to the oldValue, then it is remapped to the newValue.

void replaceAll (BiFunction<? super K, ? super V, ? extends V> function) Map

replaces all values by the values computed from this function.replaces the count of books by the letters in authors words + original count

One thought on “Java 8 – New methods in Collections

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top