Close

Java 10 - Stream Changes | New Collectors method to accumulate unmodifiable collections

[Last Updated: Mar 29, 2018]

In Java 10, following new methods have been added to java.util.stream.Collectors which input elements into an unmodifiable List/Set/Map. The resultant unmodifiable collections are created with the same characteristic as if they are created via copyOf() methods (last tutorial)

Followings are the new methods:

Collector<T, ?, List<T>> toUnmodifiableList()
Collector<T, ?, Set<T>> toUnmodifiableSet()
Collector<T, ?, Map<K,U>> toUnmodifiableMap(Function<? super T, ? extends K> keyMapper,
                                            Function<? super T, ? extends U> valueMapper)
Collector<T, ?, Map<K,U>> toUnmodifiableMap(Function<? super T, ? extends K> keyMapper,
                                            Function<? super T, ? extends U> valueMapper,
                                            BinaryOperator<U> mergeFunction)

Examples

Collectors.toUnmodifiableList()

public class ToUnmodifiableListExample {
    public static void main(String[] args) {
        List<Integer> list = IntStream.range(1, 5)
                                      .boxed()
                                      .collect(Collectors.toUnmodifiableList());
        System.out.println(list);
        System.out.println(list.getClass().getName());
    }
}
[1, 2, 3, 4]
java.util.ImmutableCollections$ListN

Collectors.toUnmodifiableSet()

public class ToUnmodifiableSetExample {
    public static void main(String[] args) {
        Set<Integer> set = IntStream.range(1, 5)
                                     .boxed()
                                     .collect(Collectors.toUnmodifiableSet());
        System.out.println(set);
        System.out.println(set.getClass().getTypeName());
    }
}
[4, 1, 2, 3]
java.util.ImmutableCollections$SetN

Collectors.toUnmodifiableMap()

There are two methods:

Collector<T, ?, Map<K,U>> toUnmodifiableMap(Function<? super T, ? extends K> keyMapper,
                                            Function<? super T, ? extends U> valueMapper)
public class ToUnmodifiableMapExample {
    public static void main(String[] args) {
        Map<Integer, Double> map =
                IntStream.range(1, 5)
                         .boxed()
                         .collect(Collectors.toUnmodifiableMap(
                                 i -> i,
                                 i -> Math.pow(i, 3))
                         );
        System.out.println(map);
        System.out.println(map.getClass().getTypeName());
    }
}
{4=64.0, 1=1.0, 3=27.0, 2=8.0}
java.util.ImmutableCollections$MapN

Here's the second method:

Collector<T, ?, Map<K,U>> toUnmodifiableMap(Function<? super T, ? extends K> keyMapper,
                                            Function<? super T, ? extends U> valueMapper,
                                            BinaryOperator<U> mergeFunction)

The mergeFunction is used to resolve collisions between values associated with the same key. Here's an example

public class ToUnmodifiableMapMergeExample {
    public static void main(String[] args) {
        Map<Integer, List<String>> map =
                Stream.of("rover", "joyful", "depth", "hunter")
                      .collect(Collectors.toUnmodifiableMap(
                              String::length,
                              List::of,
                              ToUnmodifiableMapMergeExample::mergeFunction)
                      );
        System.out.println(map);
    }

    private static List<String> mergeFunction(List<String> l1, List<String> l2) {
        List<String> list = new ArrayList<>();
        list.addAll(l1);
        list.addAll(l2);
        return list;
    }
}
{6=[joyful, hunter], 5=[rover, depth]}

Example Project

Dependencies and Technologies Used:

  • JDK 9.0.1
Java 10 Collectors New Methods Examples Select All Download
  • java-10-collectors-new-methods
    • src
      • com
        • logicbig
          • example
            • ToUnmodifiableListExample.java

    See Also