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
1 2 3 4 5 6 | 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
1 2 | temperature.removeIf(s -> s > 22); // remove elements that are > 22 |
boolean removeIf(Predicate<? super E> filter)Collection
1 2 | temperature.replaceAll(s->Math.pow(s, 0.5)); // replaces all elements by its square root |
void sort(Comparator<? super E> c)List
1 | temperature.sort((a, b) -> a > b ? -1 : 1); |
void forEach(BiConsumer<? super K, ? super V> action)Map
1 2 3 4 5 | 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
1 | authorBooks.compute("Clive Cussler", (a, b) -> b + 1); |
V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)Map
1 | authorBooks.computeIfAbsent("Agatha Christie", b -> b.length()); |
V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunctionMap
1 | authorBooks.computeIfPresent("Tom Clancy", (a, b) -> b + 1); |
V getOrDefault(Object key, V defaultValue)Map
1 | authorBooks.getOrDefault("AuthorA", 0) |
V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunctionMap
1 2 3 4 | 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
1 2 | 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
1 | authorBooks.replaceAll((a,b)->a.length()+b); |
Hi, this text contains errors, lot of them. Can they be reviewed?