beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java 9 – Factory method to create Immutable List

By Chaitanya Singh | Filed Under: Java 9 Features

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

Leave a Reply Cancel reply

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

Java 9 Tutorial

  • Java 9 features
  • JShell
  • Immutable List
  • Immutable Set
  • Immutable Map
  • Private Methods in Interface
  • Try-With-Resources Enhancement
  • Java 9 -Diamond operator Enhancement
  • @SafeVarargs annotation
  • Java 9 - Stream API Enhancements
  • Java 9 Modules

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap