Post Brief Table of Content:

  • Introduction
  • Java SE 8: Empty Immutable List
  • Java SE 9: Empty Immutable List
  • Java SE 8: Non-Empty Immutable List
  • Java SE 9: Non-Empty Immutable List
  • Characteristics of Immutable List

Introduction

Oracle Corporation is going to release Java New Version: Java SE 9 around March 2017. So, I would like to deliver a series of Posts on Java SE 9 New Features. It is my forth post in this series.

I have already delivered couple of posts on Java SE 9 New Features. Before going through this posts, please read them below :

In this post, we are going to discuss one more Java SE 9 New Feature: “Factory Methods for Immutable List” with some simple and suitable examples.

Java SE 8: Empty Immutable List

In Java SE 8 and earlier Versions, if we want to create an empty Immutable or Unmodifiable List, we should use Collections class utility method: unmodifiableList as shown below:

Example:-

NOTE:- Diamond Operator does NOT work in Java SE 6 and earlier versions. Rest of the code is same for all Java versions.

Test the above code in Java SE 9 REPL:

unmodified_empty_list-450x184

Here we can observe that to create an empty Immutable List, we need to do lot of stuff as shown above. It is very tedious and verbose thing right. Let us see the same thing in Java SE 9 now.

NOTE:- For simplicity and quick testing purpose, I’m using Java SE 9 REPL (JShell). That’s why we need an REPL for each language to test new feature quickly without doing much work.

Java SE 9: Empty Immutable List

To overcome those shortcomings, Java SE 9 has introduced a couple of useful methods in List interface so that we do not need to use all these tedious steps to create Immutable Empty List.

If we go through the Java SE 9 List API, we can find the below method signature in List interface.

It is used to create an empty Immutable List (a List with zero elements).

Example:-

Test the above code in Java SE 9 REPL:

unmodified_empty_list

Here we can observe that it is very easy to create an empty Immutable List in Java SE 9.

Java SE 8: Non-Empty Immutable List

Now we will see how to create Non-Empty Immutable List in Java SE 8 and earlier versions.

Example:-

Here also we can observe that to create a non-empty Immutable List, we need to do lot of stuff and very tedious and verbose steps. Let us see the same thing in Java SE 9 now.

Java SE 9: Non-Empty Immutable List

To overcome those shortcomings, Java SE 9 has introduced the following useful overloaded methods.

These useful methods are used to create a new Immutable List with one element to 10 elements:

The following is a Var-Args method (Variable Number of arguments method):

Example:-

It is very simple and no-verbosity in this code right. Very nice stuff!

Test the above code in Java SE 9 REPL:

Characteristics of Immutable List

As we know, Immutable Lists are not modifiable (unmodifiable collections). In this section, we will discuss the Characteristics of Immutable List(They are same in all Java versions.):

  • They are Immutable.
  • We cannot add, modify and delete their elements.
  • If we try to perform Add/Delete/Update operations on them, we will get UnsupportedOperationException as shown below:

  • They don’t allow null elements.
  • If we try to create them with null elements, we will get NullPointerException as shown below:

  • If we try add null elements, we will get UnsupportedOperationException as shown below:

  • They are serializable if all elements are serializable.

That’s it all about “Java SE 9: Factory Methods for Immutable List” concept. We will discuss some more Java SE 9 New Features in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions/type errors.

Thank you for reading my tutorials.

Happy Java SE 9 Learning!

By admin

Leave a Reply