Java9 - Collection API examples| new java features

Learn about the Java9 features - Collection API Improvements with examples.

Java9 Collection API Improvements

Java9 added minor API changes to java.util.List, java.util.Map, java.util.Set interfaces.

Added new static factory method - of(collection) and returns unmodified collection - List, Set and Map in iteration order.

This method takes a list of parameters as inputs and returns the unmodified collection of List, Set and Map respectively.

Please see for more information JEP 269🔗: Convenience Factory Methods for Collections.

Different ways to create an unmodified collection in java8?

Before the java9 version, we have many ways to create an Unmodified Collection for a few data items.

The below section contains various ways to create an Unmodified collection with examples. For these examples, using the list as a sample, the same process we can adapt to define and declare Set and Map.

  • Simple Unmodified Collection Creation

It is a tedious task to create an unmodified List, Set, or Map with few elements, and unnecessary objects got created.

First, you need to create a list object, Next constructs objects/elements, assign them with a local variable, add this element by calling add(In case of List) method put in case of Set and Map) many times, Finally, convert the normal list to an Unmodified list.

List<String> list = new ArrayList<>();
list = Collections.unmodifiableList(list);
  • Copy Constructor Unmodified Collection Create

This is another way of creating a list using the Arrays.asList method. and convert to List using ArrayList Constructor.

List<String> stringList = Collections.unmodifiableList(
new ArrayList<>(Arrays.asList("one", "two")));
  • Unmodified Collection create using Double Brace syntax

In this example, using anonymous inner classes with instances are created in double braces.

List<String> strList = Collections.unmodifiableList(new ArrayList<>() {{
add("one"); add("two"); add("three");
  • Java8 Unmodified Collection with stream

Convert the array of strings to Stream using Stream.of() method, convert the stream to List using Collectors.toList() method.

List<String> strLists = Collections.unmodifiableList(
Stream.of("one", "two", "three").collect(Collectors.toList()));

With java9, We can easily create an unmodified collection.

We will see in the following examples.

Collection API changes in java9

List and Set have both overloaded of() methods with zero to 10 arguments. Syntax and signature for List and Set are the same.

static <E> List<E> of()
static <E> List<E> of(E e1)
static <E> List<E> of(E e1,E e2)
// ..... so on upto ten parameters
static <E> List<E> of(E... e) // variable arguments
static <E> Set<E> of()
static <E> Set<E> of(E e1)
static <E> Set<E> of(E e1,E e2)
// ..... so on upto ten parameters
static <E> Set<E> of(E... e) // variable arguments

The map has also zero to 10 arguments static method.

static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)

Like List and Set, Map also has a version of the variable arguments method

static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)

List.of() Factory static method example

It is simple to create a list and the code is short.

It takes three elements like input and return list of size 3. As this method is overloaded with 10 arguments, you can supply up to 10 elements.

If more are required to insert, You can use the variable argument version of a method.

List<String> strList = List.of("one", "two", "three","four");

Set.of() factory static method example

Set. of()method takes three parameters as input and returns an unmodified collection.

Set<String> stringSet = Set.of("set1", "set2", "set3");

This method of() would not allow duplicate elements, duplicates elements throws java.lang.IllegalArgumentException: duplicate element.

Set.of("one", "two", "three", "one");

the output of the above code is

 Exception in thread "main" java.lang.IllegalArgumentException: duplicate element: one
 at java.base/java.util.ImmutableCollections$SetN.(
 at java.base/java.util.Set.of(
 at org.cloudhadoop.datetime.LamdaExpressionThisExample.main(

Map.of() Factory static method example

This interface has two methods of and ofEntries for creating the collection.

Map<Integer, String> mapDemo = Map.of(1, "one", 2, "two", 3, "three");
 // variable arguments example
Map<String, String> map = Map.ofEntries(
new AbstractMap.SimpleEntry<>("key1", "value1"),
new AbstractMap.SimpleEntry<>("key2", "value2"),
new AbstractMap.SimpleEntry<>("key3", "value3"));

Duplicate keys:

Passing Duplicate keys to Map with of() throws java.lang.IllegalArgumentException: duplicate key exception

Map.of("one", "value1", "one", "value2");

and output:

Exception in thread "main" java.lang.IllegalArgumentException: duplicate key: one
at java.base/java.util.ImmutableCollections$MapN.(
at java.base/java.util.Map.of(
at org.cloudhadoop.datetime.LamdaExpressionThisExample.main(

unmodified collection - java.lang.UnsupportedOperationException Exception:

Set<String> strSet = Set.of("one", "two");

Output is

Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.ImmutableCollections.uoe(
at java.base/java.util.ImmutableCollections$AbstractImmutableSet.add(
at org.cloudhadoop.datetime.LamdaExpressionThisExample.main(

Unmodified collection Null values not accepted in java9

of method does not accept null values.

Set.of("one", "two", null);
ist.of("one", "two", null);


Exception in thread "main" java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(
at java.base/java.util.ImmutableCollections$ListN.(
at java.base/java.util.List.of(
at org.cloudhadoop.datetime.LamdaExpressionThisExample.main(

if Null values passed as either key or value of Map, throws java.lang.UnsupportedOperationException

Set<String> strSet = Set.of("one", "two");


Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.ImmutableCollections.uoe(
at java.base/java.util.ImmutableCollections$AbstractImmutableSet.add(
at org.cloudhadoop.datetime.LamdaExpressionThisExample.main(

Collection Factory static method feature

  • It returns Unmodified Collection
  • if the elements are serialized, the returned collection is also serialized
  • No Null key and values are not allowed in Map
  • Null values are not allowed in List and Set
  • These collections are immutable, Any mutable methods(adding/modifying/deleting elements from a collection) throws UnsupportedOperationException.
  • Duplicates are not allowed and Ordering is unordered in Set
  • The list allows duplicates but the returned collection is based on the original insertion order
  • These static methods Can not be overridden. We can not use them using subclass implementation or an instance type. They can be called using interface names only.


In this tutorial, Learned java9 collection API changes and examples.