BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java 9 – Factory method to create Immutable List

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

There are couple of useful factory methods introduced in Java 9 to create immutable (unmodifiable) lists.

1. Creating immutable list prior to Java 9

Before we see the factory methods that are introduced in Java 9. Lets see how we used to create immutable lists prior to Java 9.

1.1 Creating empty immutable list before java SE 9

Prior to Java 9, we have to use unmodifiableList() method of Collections class to create immutable list.

List<String> noElementList = new ArrayList<String>();
List<String> immuList = Collections.unmodifiableList(noElementList);

Note: Lets test the code in Java Shell (JShell).
Creating immutable list before Java 9

1.2 Creating Non-empty immutable list before Java SE 9

List<String> list = new ArrayList<String>();
list.add("Chaitanya");
list.add("Rick");
list.add("Glenn");
List<String> immuList = Collections.unmodifiableList(list);

Non Empty Immutable List Prior to Java SE 9

2. Java 9 – Creating Immutable list using static factory method of()

Java 9 introduced couple of versions of of() method to create unmodifiable lists.

static <E> List<E> of()

2.1 Java 9 – Creating empty immutable list

List<String> immuList = List.of();

Java 9 Creating Empty List using of() method

2.2 Java 9 – Creating Non-empty immutable list

Lets take the same example that we have taken above using the unmodifiableList() method. As you can see how simple it is to create such lists in Java 9. We have reduced the 5 lines of code to a single line using factory method of().

List<String> immuList = List.of("Chaitanya", "Rick", "Glenn");

Java 9 creating non empty immutable list

What is an immutable List?

1. An immutable list doesn’t allow the add, delete and update of its elements.

jshell> List<String> immuList = List.of("Chaitanya", "Rick", "Glenn");
immuList ==> [Chaitanya, Rick, Glenn]

jshell> immuList.add("Negan")
|  java.lang.UnsupportedOperationException thrown: 
|        at ImmutableCollections.uoe (ImmutableCollections.java:71)
|        at ImmutableCollections$AbstractImmutableList.add 
(ImmutableCollections.java:77)
|        at (#2:1)

2. We can’t add null elements to an immutable list.

jshell> List<String> immuList = List.of("Chaitanya", "Rick", "Glenn");
immuList ==> [Chaitanya, Rick, Glenn]

jshell> immuList.add(null)
|  java.lang.UnsupportedOperationException thrown: 
|        at ImmutableCollections.uoe (ImmutableCollections.java:71)
|        at ImmutableCollections$AbstractImmutableList.add 
(ImmutableCollections.java:77)
|        at (#2:1)

UnsupportedOperationException while adding null element to an immutable list

Top Related Articles:

  1. java 9 JShell – Working with Methods
  2. Java 9 – Factory methods to create Immutable Set
  3. Java 9 Features with Examples
  4. java 9 JShell – Working with variables
  5. Java 9 – @SafeVarargs Annotation (with examples)

Tags: Java-9

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap