Java 9 List.of() Method - Create Immutable List Example

With Java 9, new factory methods are added to List, Set and Map interfaces to create immutable instances. These factory methods are convenience factory methods to create a collection in less verbose and in a concise way.
In this post, I show you how to create an immutable list using Java 9 provided List.of() static factory method.

Create Immutable List

Use List.of() static factory methods to create immutable lists. It has following different overloaded versions –
static <E> List<E>  of()
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)
//varargs
static <E> List<E>  of(E... elements)
The List instances created by these methods have the following characteristics:
  1. These lists are immutable. Elements cannot be added, removed, or replaced in these lists. Calling any mutator method (i.e. add, addAll, clear, remove, removeAll, replaceAll) will always cause UnsupportedOperationException to be thrown.
  2. They do not allow null elements. Attempts to add null elements result in NullPointerException.
  3. They are serializable if all elements are serializable.
  4. The order of elements in the list is the same as the order of the provided arguments, or of the elements in the provided array.

Create Immutable List Before Java 9 Example

Prior to Java 9, the creation of an immutable List was some kind of verbose task. For Example: 
public class ImmutableListExample {

    public static void main(String[] args) {

        // Creating an ArrayList of String using
        List < String > fruits = new ArrayList < > ();
        // Adding new elements to the ArrayList
        fruits.add("Banana");
        fruits.add("Apple");
        fruits.add("mango");
        fruits.add("orange");

        fruits = Collections.unmodifiableList(fruits);

        // Creating Immutable List
        //fruits.add("Strawberry"); // Exception in thread "main"
        // java.lang.UnsupportedOperationException<String> fruits = List.of("Banana", "Apple", "Mango", "Orange");
        fruits.forEach(e - > System.out.println(e));
    }
}
Output:
Banana
Apple
mango
orange

Create the Immutable List Example - Java 9 List.of() Method

Let's use List.of() static factory methods to create immutable lists.
package net.javaguides.corejava.java9;

import java.util.List;

/**
 * Java 9 Immutable List Example
 * @author Ramesh Fadatare
 *
 */
public class ImmutableListExample {

    public static void main(String[] args) {

        // Creating Immutable List
        List < String > fruits = List.of("Banana", "Apple", "Mango", "Orange");
        fruits.forEach(e - > System.out.println(e));

        // You can't add Elements Immutable List
        // fruits.add("Strawberry"); // Exception in thread "main" java.lang.UnsupportedOperationException

        // in single list
        List < String > list = List.of("A", "B", "C", "D");
        list.forEach(e - > System.out.println(e));
    }
}
Output:
Banana
Apple
mango
orange
A
B
C
D

Related Java 9 Posts


Comments