In this tutorial, we’re going to get through how to create an immutable Set in Java 9 by using convenience static factory methods of the Set interface, which have been added into Java 9.

1. Create An Immutable Set In Java 9 By Static Factory Methods

To create an immutable Set in Java 8, we can use the unmodifiableSet static method of Collections class, for example:

We can see that the set was created in not a simple way in Java 8. However, Java 9 offers us a more convenient way to create an immutable Set by using Static Factory Methods which have been added recently. Let’s take a look at the method syntax:

Next, let see some examples about creating an immutable Set in Java 9 by using above convenience static factory methods of the Set interface.

Java 9 also provide us the convenience static factory methods of the List interface, please visit Create Immutable Lists In Java 9 By Static Factory Methods.

2. Characteristics Of The Immutable Set Static Factory Methods

As we can see from above, the List.of() static factory methods enable us a convenient way to create immutable sets. And the Setinstances created by these methods have the following characteristics:

  • They are structurally immutable. Their elements cannot be added, removed, or replaced. If we do any mutator method, we will always get UnsupportedOperationException exception, for example:

We attempt to add one more element into the immutable Set and get exception.

  • The immutable Set can contain elements are themselves mutable, this may cause its contents to appear to change, for example:

The choices is an immutable Set which contains 2 mutables Map: japanTeaMap and noodleToppingMap, and they are can change the contents.

  • The convenience static factory methods of the Set interface disallows NULL elements. If we attempt to create them with null elements then a NullPointerException will be thrown, for example:
  • The convenience static factory methods of the Set interface rejects duplicate elements at creation time. Duplicate elements passed to a static factory method result in IllegalArgumentException, for example:
  • They are serializable if all elements are serializable and they are serialized as specified on the Serialized Form page, for example:

3. Conclusion

The tutorial has illustrated about the immutable Set static factory methods and how to use those to create immutable Sets in Java 9. We can see that the immutable methods are very convenient for us to initialize a Set from known values, and that never changes. And we always should consider using these methods if our data changes infrequently.

The sample source code presented in the tutorial is available on my Github project. It’s a Maven based project, So it’s easy to be imported in Eclipse, IntelliJ IDEA, etc.

Below are other Java 9 related tutorials for your references:

Java 9 JShell Cheat Sheet

Install Oracle Java 9 on CentOS, RHEL 7

Install Oracle Java 9 on Ubuntu 16.04 LTS (Xenial Xerus)

Private Interface Methods In Java 9

Using The InputStream.transferTo() To Copy Streams In Java 9

Streams API Updates In Java 9

How To Compare Arrays In Java 9

Java 9 – Effectively Final Variables In try-with-resources

Java 9 HTTP/2 Client API Example

 

 

0 0 vote
Article Rating