This example demonstrates how to create immutable lists with the new Java 9 Collections factory methods
With Java 9 release, Oracle is going one step foreword in acquiring working practices form other popular JVM languages like Kotlin, Guava, Scala, etc. This is done with proper respect to backwards compatibility and consistency of syntax across the JDK. One of those new Java 9 features is the creation of immutable (unmodifiable) Lists.
Prior to Java 9, the creation of immutable List was some kind of verbose task. For Example, to create an empty immutable list with pre Java 9 versions we used following code:
package javatutorial.net; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class UnmodifiableListExample { public static void main(String[] args) { List<String> emptyList = new ArrayList<>(); List<String> immutableList = Collections.unmodifiableList(emptyList); } }
What is an Immutable List
Once created immutable objects in Java can not change values. Immutable lists are no exception to this rule. You can not :
- add or remove entries from the list. If you try to do so, this will resolve in
java.lang.UnsupportedOperationException
- modify List’s elements. An attempt to do so will cause
java.lang.UnsupportedOperationException
- add null elements to the List. If you try to add a null element to the list you will end up with a
java.lang.NullPointerException
Create Empty Immutable List in Java 9
To create an empty list in Java 9, all we need to do is call the factory List method of()
, see the example below
package javatutorial.net; import java.util.List; public class Java9EmptyImmutableListExample { public static void main(String[] args) { List<String> emptyList = List.of(); } }
Create Immutable list with Elements in Java 9
There are 10 factory methods to create immutable lists up to 10 elements (source: Java 9 List interface Javadoc):
static <E> List<E> of(E e1) Returns an immutable list containing one element. static <E> List<E> of(E e1, E e2) Returns an immutable list containing two elements. static <E> List<E> of(E e1, E e2, E e3) Returns an immutable list containing three elements. static <E> List<E> of(E e1, E e2, E e3, E e4) Returns an immutable list containing four elements. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5) Returns an immutable list containing five elements. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6) Returns an immutable list containing six elements. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) Returns an immutable list containing seven elements. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) Returns an immutable list containing eight elements. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) Returns an immutable list containing nine elements. 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) Returns an immutable list containing ten elements.
and one method with variable number of arguments that let you create immutable list with desirad number of elements:
static <E> List<E> of(E... elements)
The example below creates an immutable list of 3 elements:
package javatutorial.net; import java.util.List; public class Java9ImmutableListExample { public static void main(String[] args) { List<String> list = List.of("A", "B", "C"); } }
Many of the tutorials you will find here are based on the book Mastering Java 9 written by Dr. Edward Lavieri and Peter Verhas. This book will provide you with complete and deep understanding of the new Java 9 concepts and tools.
Thank you so much very important information good job I like this article.
Looking to convert List to String Java? Let’s have a glance at how to convert a Java Collections List of elements to a String in Java.
Basically List in Java is an ordered collection or a default sequence.
We hope this article helped you to find out list to string java.You may also want to see……………..