Factory method to create Immutable List in Java SE 9
Last Updated :
07 Dec, 2022
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:
- As the name suggest these lists are immutable.
- If any attempt made to add, delete and update elements in the list we will have UnsupportedOperationException.
- Immutable list do not allow null element either.
- 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:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class ImmutableListDemo
{
public static void main(String args[])
{
List<String> gfg_empty = new ArrayList<String>();
List empty_immutable1
= Collections.unmodifiableList(gfg_empty);
List<String> gfg_non_empty = new ArrayList<String>();
gfg_non_empty.add( "write.geeksforgeeks.org" );
gfg_non_empty.add( "www.geeksforgeeks.org" );
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class ImmutableListDemo
{
public static void main(String args[])
{
List<String> gfg_empty = new ArrayList<String>();
List empty_immutable1
= Collections.unmodifiableList(gfg_empty);
empty_immutable1.add( "ide.geeksforgeeks.org" );
List<String> gfg_non_empty = new ArrayList<String>();
gfg_non_empty.add( "write.geeksforgeeks.org" );
gfg_non_empty.add( "www.geeksforgeeks.org" );
List empty_immutable2
= Collections.unmodifiableList(gfg_non_empty);
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:
import java.util.*;
class ImmutableListDemo
{
public static void main(String args[])
{
List<String> immutable = List.of();
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:
import java.util.*;
class ImmutableListDemo
{
public static void main(String args[])
{
List<String> immutable = List.of();
immutable.add( null );
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
Share your thoughts in the comments
Please Login to comment...