Skip to content
Related Articles

Related Articles

Factory method to create Immutable List in Java SE 9
  • Difficulty Level : Basic
  • Last Updated : 11 Dec, 2018
GeeksforGeeks - Summer Carnival Banner

Java 9 was released around march 2017 and In Java 9 there are some features are added in Java language and factory method for immutable List is one of them.
Characteristics of Immutable List:

    1. As the name suggest these lists are immutable.
    2. If any attempt made to add, delete and update elements in the list we will have UnsupportedOperationException.
    3. Immutable list do not allow null element either.
    4. If any attempt made to create a immutable list with null element, we will have NullPointerException. If any attempt made to add null element in list, we will have UnsupportedOperationException.

Creating immutable list in Java 8

In Java SE 8, to create immutable list in java 8 we use, java.util.Collections.unmodifiableList(List list) method.unmodifiableList(List list). This method returns an unmodifiable view of the specified list. This method allows modules to provide users with “read-only” access to internal lists.

Syntax: public static  List unmodifiableList(List list)
Returns: an unmodifiable view of the specified list.
Exception: NA

Java code for immutable empty and non-empty list in java 8:




// Java code illustrating
// creating of an 
// immutable list in Java 8
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class ImmutableListDemo
{
    public static void main(String args[])
    {
        // creating empty list
        List<String> gfg_empty = new ArrayList<String>();
          
        // making list immutable
        List empty_immutable1 
                = Collections.unmodifiableList(gfg_empty);
          
        // creating non-empty list
        List<String> gfg_non_empty = new ArrayList<String>();
  
        gfg_non_empty.add("contribute.geeksforgeeks.org");
        gfg_non_empty.add("www.geeksforgeeks.org");
          
        // making list immutable
        List empty_immutable2 
                = Collections.unmodifiableList(gfg_non_empty);
          
        System.out.println(empty_immutable1);
        System.out.println(empty_immutable2); 
    }
}

Output:



[]
[contribute.geeksforgeeks.org, www.geeksforgeeks.org]

Now lets try adding elements in the immutable list




// Java code illustrating
// an add operation 
// in immutable list in Java 8
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class ImmutableListDemo
{
    public static void main(String args[])
    {
        // creating empty list
        List<String> gfg_empty = new ArrayList<String>();
          
        // making list immutable
        List empty_immutable1 
                    = Collections.unmodifiableList(gfg_empty);
          
        // adding element in gfg_empty list
        empty_immutable1.add("ide.geeksforgeeks.org");
          
        // creating non-empty list
        List<String> gfg_non_empty = new ArrayList<String>();
  
        gfg_non_empty.add("contribute.geeksforgeeks.org");
        gfg_non_empty.add("www.geeksforgeeks.org");
          
        // making list immutable
        List empty_immutable2 
                    = Collections.unmodifiableList(gfg_non_empty);
          
        // adding element in gfg_non_empty list
        empty_immutable2.add("quiz.geeksforgeeks.org");
          
          
    }
}

Exception:

Runtime Error : Exception in thread "main" 
java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055)
at ImmutableListDemo.main(File.java:15)

Creating immutable list in Java 9

Java SE 9 has introduced couple of methods in List interface to create immutable list. To create immutable list in Java 9, we use of() method.
Java code for creating immutable list in Java 9:




// Java code illustrating
// creating immutable list
// using  of() method
import java.util.*;
class ImmutableListDemo
{
    public static void main(String args[])
    {
        // creating empty immutable list
        List<String> immutable = List.of();
           
        // creating non-empty immutable list
        List<String> immutable1 = List.of("contribute", "ide");
           
        System.out.println("empty immutable list: " + immutable);
        System.out.println("non-empty immutable list: " + immutable);
             
    }
}

Output:

empty immutable list: []
non-empty immutable list: [contribute, ide]

Lets try adding some elements in immutable list:




// Java code illustrating add 
// operation on immutable list
import java.util.*;
class ImmutableListDemo
{
    public static void main(String args[])
    {
        // creating empty immutable list
        List<String> immutable = List.of();
        immutable.add(null);
           
        // creating non-empty immutable list
        List<String> immutable1 = List.of("contribute", "ide");
        immutable1.add("install jdk 9");
           
    }
}

After running the above code, you must have obtain UnsupportedOperationException.

This article is contributed by Abhishek Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :