Immutable Collections in Java 9

1.Overview

The arrival of Java 9 brings many new features to Java’s Collections API, one of which being collection factory methods which adds syntactic sugar for creating small unmodifiable Collection instances using new convenience factory methods as per JEP 269.

In this article, we will discuss the usage and implementation details.

2. Motivation

Let’s start by looking at the problem that this is trying to solve, by instantiation a list with a few String values:

The same thing we can write using Java 5′ Arrays.asList() like below:

This is much better than the above example. But, When you try to add an element to the List, it will throw the UnsupportedOperationException. And, List can’t be mutated. If we want to mutate the value we need to iterate and change the values.

Although this List creation is better than the constructor initialization. If we want to use Set, we may need to do like below

By using Java 8 Streams

The Java 8 version, though, is a one-line expression, this method can’t be used to create a Map and its still fairly verbose.

Creating such a small immutable Collection in Java using the above approach is very verbose.

3. Collection Factories

By using  Java 9 Collection factories, we can create immutable collections.

3.1 List and Set

For example, if we want to create lists, we can do this:

We can create sets in a similar way:

The signature and characteristics of List and Set factory methods are same:

As you can see, it’s very simple, short and concise.

In the example, we have used the method with takes one to four elements as parameters and returns a List / Set of size 4. But, there are 12 overloaded versions of this method and eleven with 0 to 10 parameters and one with var-args, so that there is no fixed limit on the collection size.

3.2 Map

The signature of Map factory method is:

Example:

Similar to the List and Set, the of method is overloaded to have 0 to 10 key-value pairs.

In the case of Map, there is a different method for more than 10 key-value pairs:

Example:

4. Characteristics

The collections created using the factory methods are not the most commonly used implementations. These implementations are internal and their constructors are not made public.

Immutable: Elements cannot be added or removed. Calling any mutator method will always cause UnsupportedOperationException to be thrown

No null Element Allowed: Attempts to create them with null elements result in NullPointerException. In the case of List and Set, no elements can be null. In the case of a Map, neither keys nor values can be null.

Value-Based Instances: if we create Lists with same values, they may or may not refer to the same object on the heap.

Serialization: They are serializable if all elements are serializable.

Iteration Order: The iteration order of elements is unspecified and is subject to change.

5. Conclusion

Collection factories adding some easier syntax to perform a common operation. They give us most of the benefits without any language changes. The addition of these factory methods in Java 9 provides Immutable collections.

 

 

 

harinathk
 

Click Here to Leave a Comment Below 0 comments