Java10 Features - Unmodified Collection API changes
Java10 Collection API additions
Java10 introduced API changes to java.util.List, java.util.Map, java.util.Set
Added new static factory method - copyOf(collection) and returns unmodified collection - List,Set and Map in iteration order.
This blog post, It covers the API library changes for Collections in Java10.
Syntax and Signature
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)
Unmodified Collection
It is a collection of elements on which mutator methods like add/remove/update will not work as expected, throwing UnSupportedOperationException.
These methods throw NullPointerException if the collection is null or any element contains null values.
It allows copying the collection into other collections using this method. if the collection is modified, the return list will not get the modified changes. As this will only do the shallow clone of a collection
Java.util.list.copyOf(collection) method example
We will see the example of How to clone/copy the separate list from the existing list?
Created 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, modifying elements in collections 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)
CopyOf Notes and Features
- Collections return the unmodified collection. Elements in the collection cannot be added/removed/modified. if collection try to modify it, it throws UnsupportedOperationException
- Will does not allow null elements or null collection, trying to do with null elements, throws NullPointerException
- if elements are serialized, returned Collection is serialized
- return collection order is the same as the provided collection.
- These are static methods, So you have called them using Interface not object.