Chapter 6. Lambda Cookbook

6.1.  Develop code that uses Java SE 8 collection improvements, including Collection.removeIf(), List.replaceAll(), Map.computeIfAbsent(), and Map.computeIfPresent() methods

[Note]
  • blah

Map Enhancements

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

    It is a default method in java.util.Map interface, which attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).

    For example, this code either initializes or increments an Integer:

    
    Map<String, Integer> map = new HashMap<>();
    map.compute("A", (k, v) -> (v == null) ? 0 : v + 1);
    map.compute("A", (k, v) -> (v == null) ? 0 : v + 1);
    map.compute("B", (k, v) -> (v == null) ? 0 : v + 1);
    System.out.println(map);
    
    								

    output:

    {A=1, B=0}
    								

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

    It is a default method in java.util.Map interface, which does the following: 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 null.

    Consider the following demo:

    
    Map<String, Integer> map = new HashMap<>();
    
    Function<String, Integer> f1 = new Function<String, Integer>() {
        @Override
        public Integer apply(String t) {
            System.out.println("f1: Received " + t);
            Integer i = 0;
            System.out.println("f1: Returning " + i);
            return i;
        }
    };
    Integer i;
    i = map.get("A");
    System.out.println(i);
    i = map.computeIfAbsent("A", f1);
    System.out.println(i);
    i = map.get("A");
    System.out.println(i);
    
    								

    output:

    null
    f1: Received A
    f1: Returning 0
    0
    0
    								

  • V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

    It is a default method in java.util.Map interface, which does the following: 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.

    Consider the following demo:

    
    Map<String, Integer> map = new HashMap<>();
    
    BiFunction f1 = new BiFunction() {
        @Override
        public Object apply(Object k, Object v) {
            System.out.println("f1: Received " + k + " and " + v);
            Integer i = 0;
            System.out.println("f1: Returning " + i);
            return i;
        }
    };
    map.put("A", 1);
    Integer i;
    i = map.get("A");
    System.out.println(i);
    i = map.computeIfPresent("A", f1);
    System.out.println(i);
    i = map.get("A");
    System.out.println(i);
    
    								

    output:

    1
    f1: Received A and 1
    f1: Returning 0
    0
    0
    								

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

    It is a default method in java.util.Map interface, which performs the given action for each entry in this map until all entries have been processed or the action throws an exception.

    Consider the following demo:

    
    Map<String, Integer> map = new HashMap<>();
    map.put("A", 1);
    map.put("B", 2);
    map.put("C", 3);
    map.forEach((k, v) -> System.out.println("Key : " + k + ", value :" + v));
    
    								

    output:

    Key : A, value :1
    Key : B, value :2
    Key : C, value :3
    								

Collection Enhancements

The removeIf method can be used to remove all elements in a collection that match a predicate. Note that this is different than the filter method included in the Streams API. The filter(...) method in the Streams API produces a new Stream; it does not mutate the current stream or source.

Consider the following demo:


Set<String> set = new HashSet<>();
set.add("A");
set.add("AA");
set.add("B");
System.out.println(set);
set.removeIf(s -> s.startsWith("A"));
System.out.print(set);

					

output:

[AA, A, B]
[B]
					

List Enhancements

The replaceAll method replaces each element in a List with the result of applying a given operator to it. It is similar to the map(...) method in a Stream, but it mutates the elements of the List.

In contrast, the Stream.map(...) method produces new elements.

Consider the following demo:


List<String> list = new ArrayList<>();
list.add("A");
list.add("AA");
list.add("B");
list.replaceAll(s -> s.toLowerCase());
System.out.println(list);

					

output:

[a, aa, b]
					

Professional hosting         Free 'Oracle Certified Expert Web Services Developer 6' Guide     Exam 1Z0-810: Upgrade to Java SE 8 Programmer Quiz