Factory Methods for Collections – java 9

/, Java 9/Factory Methods for Collections – java 9

Convenience Factory Methods for Collections in JDK 9:

This article can be consider as continuation article for the Creating immutable collections before JDK 9. In previous article we explained about how to create unmodifiable collections before JDK 9 and flaws using those techniques. In this article we explained how JDK 9 solves the flaws of creating simple immutable collections. See JEP 269: Convenience Factory Methods for Collections

JDK 9 introduced of() static factory method in List, Set, Map interfaces, by using the of() static factory method of List, Set, Map interfaces and ofEntries(), and entry() static factory methods of Map interface its possible to create true immutable collections very easy.

List.of():

JDK 9 added an overloaded of() static factory method to the List interface. It provides a simple and way to create unmodifiable lists. Here are all the different versions of the of() method:

The List instances created by these methods have the following characteristics:

  1.  They are structurally immutable. Elements cannot be added, removed, or replaced. If we try to do so method will always cause UnsupportedOperationException to be thrown.
  2. They disallow null elements. Attempts to create them with null elements result in NullPointerException.
  3. The order of elements in the list is the same as the order of the provided arguments.
  4. They are serializable if all elements are serializable.
  5. The instances created by factory methods are value based. This means that factories are free to create a new instance or return an existing instance.

Immutable List example:

Output:

Set.of():

JDK 9 added an overloaded of() static factory method to the Set interface. It provides a simple and way to create unmodifiable sets. Here are all the different versions of the of() method:

The Set instances created by these methods have the following characteristics:

  1.  They are structurally immutable. Elements cannot be added, removed, or replaced. If we try to do so method will always cause UnsupportedOperationException to be thrown.
  2. They disallow null elements. Attempts to create them with null elements result in NullPointerException.
  3. They reject duplicate elements at creation time. If you try to add existing set created by factory method throws IllegalArgumentException.
  4. The iteration order of set elements is unspecified.
  5. They are serializable if all elements are serializable.
  6. The instances created by factory methods are value based. This means that factories are free to create a new instance or return an existing instance.

Immutable Set example:

Output:

Map.of():

JDK 9 added an overloaded of() static factory method to the Map interface. It provides a simple and way to create unmodifiable key values. Here are all the different versions of the of() method:

The Map instances created by these methods have the following characteristics:

  1.  They are structurally immutable. Elements cannot be added, removed, or replaced. If we try to do so method will always cause UnsupportedOperationException to be thrown.
  2. They disallow null keys. Attempts to create key with null elements result in NullPointerException.
  3. They reject duplicate keys at creation time. If you try to add duplicate key to the existing map created by factory method throws IllegalArgumentException.
  4. The iteration order of map elements is unspecified.
  5. They are serializable if all keys are serializable.
  6. The instances created by factory methods are value based. This means that factories are free to create a new instance or return an existing instance. And can not predict implementation classes that are returned by factory methods.

Immutable Map example:

Output:

Map.ofEntries():

Similarly to List and Set, the of(…) method is overloaded to have 0 to 10 key-value pairs. If more than 10 arguments List and Set interfaces has var-arg methods, in the case of Map, there is no possibility to add var-args for key values, for this Map also has different method called ofEntries(). In below example entry is a static method of Map interface and its static import in example.