Java 9 Immutable Set Example

This example demonstrates how to create immutable Set with the new Java 9 Collections factory methods

With Java 9 release, Oracle is going one step foreword in acquiring working practices form other popular JVM languages like Kotlin, Guava, Scala, etc.  This is done with proper respect to backwards compatibility and consistency of syntax across the JDK.  One of those new Java 9 features is the creation of immutable (unmodifiable) Set.

Java 9 immutable Set example

Java 9 immutable Set example

Prior to Java 9, the creation of immutable Set was some kind of verbose task. For Example, to create an empty immutable Set with pre Java 9 versions we used following code:

package javatutorial.net;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class UnmodifiableSetExample {

	public static void main(String[] args) {
		Set<String> emptySet = new HashSet<String>();
		Set<String> immutableSet = Collections.unmodifiableSet(emptySet);
	}

}




What is an Immutable Set

Once created immutable objects in Java can not change values. Immutable Sets are no exception to this rule. You can not :

  • add or remove entries from the set. If you try to do so, this will resolve in java.lang.UnsupportedOperationException
  • modify Set elements. An attempt to do so will cause java.lang.UnsupportedOperationException
  • add null elements to the Set. If you try to add a null element to the set you will end up with a java.lang.NullPointerException

Create Empty Immutable Set in Java 9

To create an empty list in Java 9, all we need to do is call the factory List method of(), see the example below

package javatutorial.net;

import java.util.Set;

public class Java9EmptyImmutableSetExample {
	public static void main(String[] args) {
		Set<String> emptySet = Set.of();
	}
}

Create Immutable Set with Elements in Java 9

There are 10 factory methods to create immutable Sets up to 10 elements (source: Java 9 Set interface Javadoc):

static <E> Set<E>	of​(E e1)	
Returns an immutable set containing one element.

static <E> Set<E>	of​(E e1, E e2)	
Returns an immutable set containing two elements.

static <E> Set<E>	of​(E e1, E e2, E e3)	
Returns an immutable set containing three elements.

static <E> Set<E>	of​(E e1, E e2, E e3, E e4)	
Returns an immutable set containing four elements.

static <E> Set<E>	of​(E e1, E e2, E e3, E e4, E e5)	
Returns an immutable set containing five elements.

static <E> Set<E>	of​(E e1, E e2, E e3, E e4, E e5, E e6)	
Returns an immutable set containing six elements.

static <E> Set<E>	of​(E e1, E e2, E e3, E e4, E e5, E e6, E e7)	
Returns an immutable set containing seven elements.

static <E> Set<E>	of​(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)	
Returns an immutable set containing eight elements.

static <E> Set<E>	of​(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)	
Returns an immutable set containing nine elements.

static <E> Set<E>	of​(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)	
Returns an immutable set containing ten elements.

and one method with variable number of arguments that let you create immutable set with arbitrary number of elements:

static <E> Set<E>	of​(E... elements)

The example below creates an immutable Set of 3 elements:

package javatutorial.net;

import java.util.Set;

public class Java9ImmutableSetExample {
	public static void main(String[] args) {
		Set set = Set.of("A", "B", "C");
	}
}

 

Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Digg thisPin on PinterestShare on RedditShare on StumbleUponPrint this pageEmail this to someone

Leave a Reply

Be the First to Comment!

avatar
wpDiscuz