Factory Methods for Immutable List, Set, Map and Map.Entry

Java 9 introduced a new way to create immutable collections in java. If you are a beginner then you should read about immutable collection in java(Immutable list, immutable set etc). Java 9 provides Factory methods to create Immutable List, Set, Map and Map.Entry objects. These methods are known as utility methods in java and are used to create empty or non-empty Collection objects.

Java immutable collections java 9

Even Java 8 provides a way to create an immutable List, Set, Map etc. But Java 9 makes it more easier and flexible. In java 8, we use the Collections class to create immutable collections and this approach takes some extra steps. In the next sections, we will how the java 9 factory method uses and Java immutable collections java 9.

The need for Java 8 factory method

As we know, Java 8 provides collections class to create an immutable List, Set, Map etc. Here we will see how to do java 8 works and the problem areas.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ImmutableListJava8 
{
	public static void main(String arg[])
	{
		List<String> units = new ArrayList<>();
		units.add("One");
		units.add("Two");
		units.add("Three");
		units.add("Four");
		List<String> immutableList = Collections.unmodifiableList(units);
		System.out.println(immutableList);
	}
}

Output: [One, Two, Three, Four]

We can use the asList() method of the Arrays class, which will be much better than the above approach.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ImmutableListJava8 
{
	public static void main(String arg[])
	{
		List<String> units = Arrays.asList("One", "Two", "Three", "Four");
		List<String> immutableList = Collections.unmodifiableList(units);
		System.out.println(immutableList);
	}
}

Output: [One, Two, Three, Four]

But when we want to create ImmutableSet, then it takes more steps.

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class ImmutableListJava8 
{
	public static void main(String arg[])
	{
		Set<String> units = new HashSet<>(Arrays.asList("One", "Two", "Three", "Four"));
		Set<String> immutableSet = Collections.unmodifiableSet(units);
		System.out.println(immutableSet);
	}
}

Output: [One, Two, Three, Four]

Let’s see how Java 9 factory methods resolve these problems.

Java 9 – Factory method to create Immutable List

Java 9 provides some Factory methods that are used to create an immutable list in java. All these methods are presented in the List interface. The List interface contains of() method and it is an overloaded method that can take 1 to 10 parameters. The of() method doesn’t accept null, if we add any null it throws NullPointerException. All these methods are static so we can access them by class name.

These methods are used to create empty Immutable List and non-empty Immutable List.

static <E> List<E> of(E e1) 
static <E> List<E> of(E e1,E e2) 
static <E> List<E> of(E e1,E e2,E e3) 
static <E> List<E> of(E e1,E e2,E e3,E e4) 
static <E> List<E> of(E e1,E e2,E e3,E e4,E e5) 
static <E> List<E> of(E e1,E e2,E e3,E e4,E e5,E e6) 
static <E> List<E> of(E e1,E e2,E e3,E e4,E e5,E e6,E e7) 
static <E> List<E> of(E e1,E e2,E e3,E e4,E e5,E e6,E e7,E e8) 
static <E> List<E> of(E e1,E e2,E e3,E e4,E e5,E e6,E e7,E e8,E e9) 
static <E> List<E> of(E e1,E e2,E e3,E e4,E e5,E e6,E e7,E e8,E e9,E e10)
import java.util.List;

public class ImmutableListJava8 
{
	public static void main(String arg[])
	{
		// Creating empty immutable list 
		List<String> immutableList = List.of();
		
		// Creating non-empty immutable list 
		List<String> immutableList1 = List.of("1");
		
		List<String> immutableList2 = List.of("1", "2");
		List<String> immutableList3 = List.of("1", "2", "3");
		List<String> immutableList4 = List.of("1", "2", "3", "4");
		List<String> immutableList5 = List.of("1", "2", "3", "4", "5");
		List<String> immutableList6 = List.of("1", "2", "3", "4", "5", "6");
		List<String> immutableList7 = List.of("1", "2", "3", "4", "5", "7");
		List<String> immutableList8 = List.of("1", "2", "3", "4", "5", "7", "8");
		List<String> immutableList9 = List.of("1", "2", "3", "4", "5", "7", "8", "9");
		List<String> immutableList10 = List.of("1", "2", "3", "4", "5", "7", "8", "9", "10");
		
		System.out.println(immutableList);
		System.out.println(immutableList1);
		System.out.println(immutableList2);
		System.out.println(immutableList3);
		System.out.println(immutableList4);
		System.out.println(immutableList5);
		System.out.println(immutableList6);
		System.out.println(immutableList7);
		System.out.println(immutableList8);
		System.out.println(immutableList9);
		System.out.println(immutableList10);
	}
}

Output: []
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 7]
[1, 2, 3, 4, 5, 7, 8]
[1, 2, 3, 4, 5, 7, 8, 9]
[1, 2, 3, 4, 5, 7, 8, 9, 10]

Java 9 – Factory method to create Immutable Set

Like Immutable List, Java 9 provides some methods to create an Immutable Set. These methods are defined in the Set interface with the static keyword, so all these methods are static methods. The Set interface provides of() method, which is the overload method and it takes 1 to 10 parameters. But it also provides a method that can accept any number of elements and a single array and returns the Immutable Set.

We can create an empty and non-empty immutable Set by use of() method. This method doesn’t take null, if we try to add null, it throws NullPointerException.

static <E> Set<E> of(E e1) 
static <E> Set<E> of(E e1,E e2) 
static <E> Set<E> of(E e1,E e2,E e3) 
static <E> Set<E> of(E e1,E e2,E e3,E e4) 
static <E> Set<E> of(E e1,E e2,E e3,E e4,E e5) 
static <E> Set<E> of(E e1,E e2,E e3,E e4,E e5,E e6) 
static <E> Set<E> of(E e1,E e2,E e3,E e4,E e5,E e6,E e7) 
static <E> Set<E> of(E e1,E e2,E e3,E e4,E e5,E e6,E e7,E e8) 
static <E> Set<E> of(E e1,E e2,E e3,E e4,E e5,E e6,E e7,E e8,E e9) 
static <E> Set<E> of(E e1,E e2,E e3,E e4,E e5,E e6,E e7,E e8,E e9,E e10)
import java.util.List;
import java.util.Set;

public class ImmutableListJava9 
{
	public static void main(String arg[])
	{
		// Creating empty immutable Set 
		Set<String> immutableSet = Set.of();
		
		// Creating non-empty immutable list 
		Set<String> immutableSet1 = Set.of("1");
		
		Set<String> immutableSet2 = Set.of("1", "2");
		Set<String> immutableSet3 = Set.of("1", "2", "3");
		Set<String> immutableSet4 = Set.of("1", "2", "3", "4");
		Set<String> immutableSet5 = Set.of("1", "2", "3", "4", "5");
		Set<String> immutableSet6 = Set.of("1", "2", "3", "4", "5", "6");
		Set<String> immutableSet7 = Set.of("1", "2", "3", "4", "5", "7");
		Set<String> immutableSet8 = Set.of("1", "2", "3", "4", "5", "7", "8");
		Set<String> immutableSet9 = Set.of("1", "2", "3", "4", "5", "7", "8", "9");
		Set<String> immutableSet10 = Set.of("1", "2", "3", "4", "5", "7", "8", "9", "10");
		
		System.out.println(immutableSet);
		System.out.println(immutableSet1);
		System.out.println(immutableSet2);
		System.out.println(immutableSet3);
		System.out.println(immutableSet4);
		System.out.println(immutableSet5);
		System.out.println(immutableSet6);
		System.out.println(immutableSet7);
		System.out.println(immutableSet8);
		System.out.println(immutableSet9);
		System.out.println(immutableSet10);
	}
}

Output: []
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 7]
[1, 2, 3, 4, 5, 7, 8]
[1, 2, 3, 4, 5, 7, 8, 9]
[1, 2, 3, 4, 5, 7, 8, 9, 10]

Immutable Non-Empty Set with Var-Args

This method can take any number of parameters or a single type of array. It returns an immutable set and the size of the set will be the same as the array. It doesn’t accept any null value.

import java.util.Set;

public class ImmutableListJava9 
{
	public static void main(String arg[])
	{
		Set<String> immutableSet1 = Set.of("1", "2", "3", "4", "5", "7", "8", "9", "10", "11");
		String array[] = {"1", "2", "3", "4", "5", "7", "8", "9", "10", "11"};
		Set<String> immutableSet2 = Set.of(array);
		System.out.println(immutableSet1);
		System.out.println(immutableSet2);
	}
}

Output: [8, 7, 5, 4, 3, 2, 1, 11, 10, 9]
[8, 7, 5, 4, 3, 2, 1, 11, 10, 9]

Java 9 – Factory method to create Immutable Map

Java 9 has of() method that is used to create an immutable map. This is an overloaded method that can accept 1 to 10 parameters. This method takes key and value that is known as a pair. If you want to create an immutable map by of() method, then you have to provide the key and value.

You can create an empty and non-empty immutable map by use of() method.

static <K,V> Map<K,V> of(K k1, V v1) 
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2) 
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3) 
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) 
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) 
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) 
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) 
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)
import java.util.Map;

public class ImmutableListJava9 
{
	public static void main(String arg[])
	{
		Map immutableMap = Map.of();
		Map immutableMap1 = Map.of(1, "One");
		Map immutableMap2 = Map.of(1, "One", 2, "Two");
		Map immutableMap3 = Map.of(1, "One", 2, "Two", 3, "Three");
		Map immutableMap4 = Map.of(1, "One", 2, "Two", 3, "Three", 4, "four");
		Map immutableMap5 = Map.of(1, "One", 2, "Two", 3, "Three", 4, "four", 5, "Five");
		Map immutableMap6 = Map.of(1, "One", 2, "Two", 3, "Three", 4, "four", 5, "Five", 6, "Six");
		Map immutableMap7 = Map.of(1, "One", 2, "Two", 3, "Three", 4, "four", 5, "Five", 6, "Six", 7, "Seven");
		Map immutableMap8 = Map.of(1, "One", 2, "Two", 3, "Three", 4, "four", 5, "Five", 6, "Six", 7, "Seven", 8, "Eight");
		Map immutableMap9 = Map.of(1, "One", 2, "Two", 3, "Three", 4, "four", 5, "Five", 6, "Six", 7, "Seven", 8, "Eight", 9, "Nine");
		Map immutableMap10 = Map.of(1, "One", 2, "Two", 3, "Three", 4, "four", 5, "Five", 6, "Six", 7, "Seven", 8, "Eight", 9, "Nine", 10, "Ten");

		System.out.println(immutableMap);
		System.out.println(immutableMap1);
		System.out.println(immutableMap2);
		System.out.println(immutableMap3);
		System.out.println(immutableMap4);
		System.out.println(immutableMap5);
		System.out.println(immutableMap6);
		System.out.println(immutableMap7);
		System.out.println(immutableMap8);
		System.out.println(immutableMap9);
		System.out.println(immutableMap10);
	}
}

Output: {}
{1=One}
{1=One, 2=Two}
{1=One, 2=Two, 3=Three}
{4=four, 1=One, 2=Two, 3=Three}
{2=Two, 3=Three, 4=four, 5=Five, 1=One}
{1=One, 2=Two, 3=Three, 4=four, 5=Five, 6=Six}
{1=One, 2=Two, 3=Three, 4=four, 5=Five, 6=Six, 7=Seven}
{4=four, 5=Five, 6=Six, 7=Seven, 8=Eight, 1=One, 2=Two, 3=Three}
{6=Six, 7=Seven, 8=Eight, 9=Nine, 1=One, 2=Two, 3=Three, 4=four, 5=Five}
{1=One, 2=Two, 3=Three, 4=four, 5=Five, 6=Six, 7=Seven, 8=Eight, 9=Nine, 10=Ten}

Leave a Comment