BIG
DATA

JAVA

Convenience Factory methods for collections in Java 9

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

JEP 269: Convenience Factory Methods for Collections in Java 9

The motive of this JEP(JDK Enhancement Proposal) is to provide static factory methods on the collection interfaces that will create compact, unmodifiable collection instances. Define library APIs to make it convenient to create instances of collections and maps with small numbers of elements, so as to ease the pain of not having collection literals in the Java programming language.


An object is considered immutable if its state cannot change after it is constructed. After you create an immutable instance of a collection, it holds the same data as long as a reference to it exists.

In JDK 9, convenience static factory methods on the List, Set, and Map interfaces were added which let you easily create immutable lists, sets, and maps. If the collections created using these methods contain immutable objects, then they are automatically thread safe after construction. But if an immutable collection contains mutable objects, then collection is neither immutable nor thread safe.

Immutable List Static Factory Methods

The List.of() static factory methods provide a convenient way to create immutable lists. The List instances created by these methods have the following characteristics:

  • They are structurally immutable. Elements cannot be added, removed, or replaced. Calling any mutator method will always cause UnsupportedOperationException to be thrown. However, if the contained elements are themselves mutable, this may cause the List's contents to appear to change.
  • They disallow null elements. Attempts to create them with null elements result in NullPointerException.

The syntax of these methods is:

List.of()
List.of(e1)
List.of(e1, e2)
List.of(elements...)   // varargs form supports an arbitrary number of elements or an array

Lets see how this is different from other release.
In JDK 8:

List<String> stringList = Arrays.asList("a", "b", "c");
stringList = Collections.unmodifiableList(stringList);

In JDK 9:

List stringList = List.of("a", "b", "c");

Immutable Set Static Factory Methods

The Set.of() static factory methods provide a convenient way to create immutable sets. The Set instances created by these methods have the following characteristics:

  • They are structurally immutable. Elements cannot be added or removed. Calling any mutator method will always cause UnsupportedOperationException to be thrown. However, if the contained elements are themselves mutable, this may cause the Set to behave inconsistently or its contents to appear to change.
  • They disallow null elements. Attempts to create them with null elements result in NullPointerException.

The syntax of these methods is:

Set.of()
Set.of(e1)
Set.of(e1, e2)         // fixed-argument form overloads up to 10 elements
Set.of(elements...)   // varargs form supports an arbitrary number of elements or an array

Lets see how this is different from other release.
In JDK 8:

Set<String> stringSet = new HashSet<>(Arrays.asList("a", "b", "c"));
stringSet = Collections.unmodifiableSet(stringSet);

In JDK 9:

Set<String> stringSet = Set.of("a", "b", "c");

Immutable Map Static Factory Methods

The Map.of() and Map.ofEntries() static factory methods provide a convenient way to create immutable maps. The Map instances created by these methods have the following characteristics:

  • They are structurally immutable. Keys and values cannot be added, removed, or updated. Calling any mutator method will always cause UnsupportedOperationException to be thrown. However, if the contained keys or values are themselves mutable, this may cause the Map to behave inconsistently or its contents to appear to change.
  • They disallow null keys and values. Attempts to create them with null keys or values result in NullPointerException.

The syntax of these methods is:

Map.of()
Map.of(k1, v1)
Map.of(k1, v1, k2, v2)    // fixed-argument form overloads up to 10 key-value pairs
Map.ofEntries(entry(k1, v1), entry(k2, v2),...)
 // varargs form supports an arbitrary number of Entry objects or an array

Lets see how this is different from other release.
In JDK 8:

Map<String, Integer> stringMap = new HashMap<String, Integer>(); 
stringMap.put("a", 1); 
stringMap.put("b", 2);
stringMap.put("c", 3);
stringMap = Collections.unmodifiableMap(stringMap);

In JDK 9:

Map stringMap = Map.of("a", 1, "b", 2, "c", 3);

If you have more than 10 key-value pairs, then create the map entries using the Map.entry method, and pass those objects to the Map.ofEntries method. For example:

import static java.util.Map.entry;
Map <Integer, String> friendMap = Map.ofEntries(
   entry(1, "Tom"),
   entry(2, "Dick"),
   entry(3, "Harry"),
   ...
   entry(99, "Mathilde"));