Open In App

Factory method to create Immutable List in Java SE 9

Last Updated : 07 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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("write.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:

[]
[write.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("write.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("www.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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads