Java 9 : Collections factory methods

As of Java 8, there’s  no simple or single statement way of creating collections with a small number of elements. In scenarios like unit tests where a Set of Strings are to be created as mock data,  it is needed to create a new Set and start repeatedly adding the elements which is highly verbose. Java 9 simplifies it through varargs based factory methods to create collections with a small number of elements. List, Set and Map interfaces have of method that creates the respective immutable Collection/Map. These convenience methods are implemented based on varargs

Creating a List

Prior to Java 9, immutable list is created using

List<String> immutableList = Arrays.asList("Cersei", "Hound", "Ilyn Payne", "Melissandre", "Beric Dondarrion");

if we add an element to the immutableList,

immutableList.add("Walder Frey");

an UnsupportedOperationException is thrown. This is OK as we expect the list to be immutable. But the List returned by Arrays.asList is vulnerable to set operation

immutableList.set(2, "Walder Frey");

after this operation, Ilyn Payne is replaced by Walder Frey.

in Java 9 an immutable list can be created using

List<String> immuatbleListJ9 = List.of("Cersei", "Hound", "Ilyn Payne", "Melissandre", "Beric Dondarrion");

if we add an element to the immutableListJ9

immutableListJ9.add("Walder Frey");

an UnsupportedOperationException is thrown as expected.

if we try set operation

immutableListJ9.set(2, "Walder Frey");

an UnsupportedOperationExceptionon is thrown.

Creating a Set

Prior to Java 9, there’s no easy way to create a Set
One way is to create a new Set and add elements one by one

Set<String> theSet = new HashSet&lt;&gt;();
theSet.add("Ilyn Payne");

other way is to use

Set<String> theSet = new HashSet<>(Arrays.asList("Cersei", "Hound", "Ilyn Payne", "Melissandre", "Beric Dondarrion")); 

The Set created in above ways is mutable, we can keep adding or removing elements to or from it. This Set can be converted to immutable with the help of Collections.unmodifiableSet

in Java 9, the Set can be created using

Set<String> theSetJ9 = Set.of("Cersei", "Hound", "Ilyn Payne", "Melissandre", "Beric Dondarrion");

The Set created using factory method is an immutable set, it con’t be modified. If try to add or remove an element to or from this set, an UnsupportedOperationException is thrown.

Creating a Map

Prior to Java 9, Map also doesn’t have a easy way to create

Map<Integer, String> theMap = new HashMap&gt;();
theMap.put(1, "Cersei");
theMap.put(2, "Hound");
theMap.put(3, "Ilyn Payne");

The Map created in above way is mutable, we can keep adding or removing entries to or from it. This map can be converted to immutable with the help of Collections.unmodifiableMap

in Java 9, the Map can be created using

Map<Integer, String> theMapJ9 = Map.of(1, "Cersei", 2, "Hound", 3, "Ilyn Payne", 4, "Melissandre",
5, "Beric Dondarrion");

Map.of() method expects the arguments in key1, value, key2, value2 …. keyn, valuen order. As the map needs two different types of elements, Map.of method doesn’t take varags instead it has over loaded versions that takes upto 10 key-value pairs. Map has another version that takes the varags of Map.Entry<K,V>; Map.ofEntries(Map.Entry<K,V> ....). To facilitate the easy creation of Map.Entry objects, another convenience method, Map.entry() is added to the Map interface. Using these two convenience methods a Map object can be created

Map<Integer, String> theMapJ9 = Map.ofEntries(entry(1, "Cersei"), entry(2, "Hound"), entry(3, "Ilyn Payne"),
entry(4, "Melissandre"), entry(5, "Beric Dondarrion")); 

If we perform a put operation on this map, an UnsupportedOperationException is thrown.

The convenience factory methods of List, Set and Map interfaces provides an easy way to create an instance of respective Collection or Map with a small number of elements. The Objects returned by these methods are immutable, any operation trying to modify them will throw an UnsupportedOperationException.