Factory method to create Immutable Collection in Java 9

Last updated on February 27th, 2024

In Java 9 we can use “of()” factory methods to create immutable List, Set and Map. Until Java 8 we have to write a lot of lines of code to create immutable Lists, Sets and Maps. But from Java 9 onwards we can create an immutable List, Set and Map with a single line of code with factory methods.

Table of content

  1. How to create Immutable List?
  2. How to create Immutable Set?
  3. How to create ImmutableMap?
  4. Conclusion

1. How to create Immutable List?

We can create immutable Lists by using List.of() factory methods in Java 9. There are 12 overloaded “of()” factory methods of List in Java 9:

public static <E> List<E>  of()
public static <E> List<E>  of(E e1)
public static <E> List<E>  of(E e1, E e2)
public static <E> List<E>  of(E e1, E e2, E e3)
--------------------------
--------------------------
--------------------------
//varargs
public static <E> List<E>  of(E... elements)

Example

import java.util.List;
public class ImmutableList 
{
    public static void main(String[] args) 
    {
        List<String> names = List.of("Peter", "Robert", "William");
        //Preserve order list elements
        System.out.println(names);
         //UnsupportedOperationException occured
        //names.add("Robbin"); 
        //NullPointerException
        //List<String> names2 = List.of("Peter", "Robert", "William", null); 
    }
}

We cannot add, remove, or replace elements in the immutable List. Calling any mutator method of List will always cause UnsupportedOperationException to be thrown in the immutable List.
Do not allow null elements in an immutable List. If we are adding null elements in an immutable List then NullPointerException is raised.
Output

[Peter, Robert, William]

2. How to create Immutable Set?

We can create immutable Sets by using Set.of() factory methods in Java 9. There 12 several overloaded “of()” factory methods of Set in Java 9:

public static <E> Set<E> of()
public static <E> Set<E> of(E e1)
public static <E> Set<E> of(E e1, E e2)
public static <E> Set<E> of(E e1, E e2, E e3)
--------------------------
--------------------------
--------------------------
//varargs
public static <E> Set<E> of(E... elements)

Example

import java.util.Set;
public class ImmutableSet 
{
    public static void main(String[] args) 
    {
        Set<String> names = Set.of("Peter", "Robert", "William");
        System.out.println(names);
        //UnsupportedOperationException occured 
        //names.add("Robbin"); 
        //NullPointerException
        //Set<String> names2 = Set.of("Peter", "Robert", "William", null); 
         //IllegalArgumentException
        //Set<String> names3 = Set.of(Peter", "Robert", "William", ?Peter?); 
    }
}

Output

[Robert, William, Peter]

3. How to create ImmutableMap?

We can create immutable Maps by using Map.of() factory methods in Java 9. There 11 several overloaded “of()” factory methods of Map in Java 9:

public static <K,V> Map<K,V> of()
public static <K,V> Map<K,V> of(K k1, V v1)
---------------------------------
---------------------------------
---------------------------------
public static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)

Example

import java.util.Map;
 public class ImmutableMap {
    public static void main(String[] args) {
        Map<String, String> names = Map.ofEntries(
                Map.entry("1", "Peter"),
                Map.entry("2", "Robert"),
                Map.entry("3", "William"));
        System.out.println(names);
        //UnsupportedOperationException
        //names.put("2", "Ravi");
    }
}

Output

{2=Robert, 3=William, 1=Peter}

4. Conclusion

In this topic, we learnt about How to create Immutable List in Java 9? How to create Immutable Set in Java 9? How to create Immutable Map in Java 9?

Leave a Comment