Introduction
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
List<Double> temperature = new ArrayList<Double<(Arrays.asList(new Double[] { 20.0, 22.0, 22.5 })); temperature.forEach(s -> System.out.println(s)); // prints the number in separate lines // or written using method references temperature.forEach(System.out::println);
boolean removeIf(Predicate<? super E> filter) Collection
temperature.removeIf(s -> s > 22); // remove elements that are > 22
boolean removeIf(Predicate<? super E> filter)Collection
temperature.replaceAll(s->Math.pow(s, 0.5)); // replaces all elements by its square root
void sort(Comparator<? super E> c)List
temperature.sort((a, b) -> a > b ? -1 : 1);
void forEach(BiConsumer<? super K, ? super V> action)Map
authorBooks.forEach((a, b) -> System.out.println(a + " wrote " + b + " books")); Map<String , Integer> authorBooks = new HashMap<String , Integer>(); authorBooks.put("Robert Ludlum", 27); authorBooks.put("Clive Cussler", 50); authorBooks.put("Tom Clancy", 17);
V compute(K key,BiFunction<? super K, ? super V, ? extends V> remappingFunction)List
authorBooks.compute(“Clive Cussler”, (a, b) -> b + 1);
If 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
authorBooks.computeIfAbsent("Agatha Christie", b -> b.length());
The entry is added only if the computed value is not null.
V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunctionMap
authorBooks.computeIfPresent("Tom Clancy", (a, b) -> b + 1);
V getOrDefault(Object key, V defaultValue)Map
authorBooks.getOrDefault("AuthorA", 0)
the map does not contain ‘AuthorA’ so this returns 0.
V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunctionMap
authorBooks.merge("AuthorB", 1, (a, b) -> a + b); System.out.println(authorBooks.get("AuthorB"));// 1 authorBooks.merge("AuthorB", 1, (a, b) -> a + b); System.out.println(authorBooks.get("AuthorB"));//2
V putIfAbsent(K key, V value)Map
System.out.println(authorBooks.putIfAbsent("AuthorC", 2));//null System.out.println(authorBooks.putIfAbsent("AuthorC", 2));//2
boolean remove(Object key, Object value) Map
V replace(K key, V newValue) Map
boolean replace(K key, V oldValue, V newValue) Map
void replaceAll (BiFunction<? super K, ? super V, ? extends V> function) Map
authorBooks.replaceAll((a,b)->a.length()+b);
replaces the count of books by the letters in authors words + original count
Hi, this text contains errors, lot of them. Can they be reviewed?