Java10 Features - Unmodified Collection API changes

Java 10 Collection API Additions

Java 10 introduced API changes to java.util.List, java.util.Map, and java.util.Set.

A new static factory method, copyOf(collection), was added. It returns an unmodified collection - List, Set, and Map in iteration order.

This blog post covers the API library changes for Collections in Java 10.

static <E> List<E> copyOf​(Collection<? extends E> coll)
static <K,V> Map<K,V> copyOf​(Map<? extends K,? extends V> map)
static <E> Set<E> copyOf​(Collection<? extends E> coll)

What is Unmodified Collection

This is a collection of elements on which mutator methods like add/remove/update will not work as expected, as they throw UnsupportedOperationException. These methods will also throw NullPointerException if the collection is null or any element contains null values. Additionally, this collection allows copying into other collections using a specific method. However, if the original collection is modified, the returned list will not reflect these changes. This is because the method only performs a shallow clone of the collection.

Java.util.list.copyOf(collection) method example

Let’s explore an example of how to clone or copy a separate list from an existing list.

First, we create a list with duplicate elements.

the returned list contains the same order like the order provided a list of the insertion order. It allows duplicate elements.

List<String> stringList = new ArrayList<>();
stringList.add("one");
stringList.add("two");
stringList.add("two");
stringList.add("three");
List<String> newStringList = List.copyOf(stringList);
newStringList.stream().forEach(System.out::println);

The output of the above code is

one
two
two
three

Java.util.Set.copyOf(collection) method example

We will see the example of How to clone/copy a separate set from the existing list? Created a List with duplicate elements. returned set contains that does not allow duplicate elements. the order is not guaranteed.

List<String> stringList = new ArrayList<>();
stringList.add("one");
stringList.add("two");
stringList.add("two");
stringList.add("three");
Set<String> newStringList = Set.copyOf(stringList);
newStringList.stream().forEach(System.out::println);

The output of the above code execution is

one
three
two

Java.util.Map.copyOf(collection) method example

We will see the example of How to clone/copy a separate Map from the existing Map? Created a Map with duplicate key elements. returned Map contains that does not allow duplicate keys. the order is based on a hashing algorithm.

Map<String, String> mapString = new HashMap<>();
mapString.put("key1","value1");
mapString.put("key2","value2");
mapString.put("key2","value23");
mapString.put("key3","value3");
Map<String, String> newMapString = Map.copyOf(mapString);
System.out.println(newMapString);

The output of the above code execution is

{key3=value3, key1=value1, key2=value23}

copyOf UnsupportedOperationException Exception example

copyOf method returns an unmodified collection, If elements in collections modifieds throw UnsupportedOperationException

import java.util.ArrayList;
import java.util.List;
public class TestInterface {
 public static void main(String[] args) {
  List<String> stringList = new ArrayList<>();
  stringList.add("one");
  stringList.add("two");
  stringList.add("two");
  stringList.add("three");
  modifyCollection(List.copyOf(stringList));
 }
 public static void modifyCollection(List<String> strings) {
  strings.set(0, "newone");
 }
}

Output is

Exception in thread "main" java.lang.UnsupportedOperationException
 at java.base/java.util.AbstractList.set(AbstractList.java:136)
 at org.cloudhadoop.datetime.TestInterface.modifyCollection(TestInterface.java:49)
 at org.cloudhadoop.datetime.TestInterface.main(TestInterface.java:24)

Notes:

  • Collections return the unmodified collection. Elements in the collection cannot be added, removed, or modified. If an attempt is made to modify the collection, it throws an UnsupportedOperationException.
  • It does not allow null elements or a null collection. Attempting to use null elements will result in a NullPointerException.
  • If elements are serialized, the returned Collection is also serialized.
  • The order of the returned collection is the same as the provided collection.
  • These are static methods, so they should be called using the interface rather than an object.