Home » Java 9 Collection Factory Methods

Java 9 Collection Factory Methods

by Online Tutorials Library

Java 9 Factory Methods

Java 9 Collection library includes static factory methods for List, Set and Map interface. These methods are useful to create small number of collection.

Suppose, if we want to create a list of 5 elements, we need to write the following code.


Java List Example

Output:

Java  JavaFX  Spring  Hibernate  JSP  

In the above code, add method is called repeatedly for each list element, while in Java 9 we can do it in single line of code using factory methods.


Factory Methods for Collection

Factory methods are special type of static methods that are used to create unmodifiable instances of collections. It means we can use these methods to create list, set and map of small number of elements.

It is unmodifiable, so adding new element will throw java.lang.UnsupportedOperationException

Each interface has it’s own factory methods, we are listing all the methods in the following tables.


Factory Methods of List Interface

Modifiers Methods Description
static <E> List<E> Of() It It returns an immutable list containing zero elements.
static <E> List<E> of(E e1) It It returns an immutable list containing one element.
static <E> List<E> of(E… elements) It It returns an immutable list containing an arbitrary number of elements.
static <E> List<E> of(E e1, E e2) It It returns an immutable list containing two elements.
static <E> List<E> of(E e1, E e2, E e3) It It returns an immutable list containing three elements.
static <E> List<E> of(E e1, E e2, E e3, E e4) It It returns an immutable list containing four elements.
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5) It It returns an immutable list containing five elements.
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6) It It returns an immutable list containing six elements.
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) It It returns an immutable list containing seven elements.
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) It It returns an immutable list containing eight elements.
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) It It returns an immutable list containing nine elements.
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) It It returns an immutable list containing ten elements.

Java 9 List Factory Method Example

In Java 9, we can write this code in vary simple manner with the help of List.of() factory method.

Output:

Java  JavaFX  Spring  Hibernate  JSP  

Java 9 Set Interface

Java Set interface provides a Set.of() static factory method which is used to create immutable set. The set instance created by this method has the following characteristcis.

  • It is immutable
  • No null elements
  • It is serializable if all elements are serializable.
  • No duplicate elements.
  • The iteration order of set elements is unspecified and is subject to change.

Java 9 Set Interface Factory Methods

The following table contains the factory methods for Set interface.

(e>

Modifier and Type Method Description
static <E> Set<E> of() It It returns an immutable set containing zero elements.
static <E> Set<E> of(E e1) It It returns an immutable set containing one element.
static <E> Set<E> of(E… elements) It It returns an immutable set containing an arbitrary number of elements.
static <E> Set<E> of(E e1, E e2) It It returns an immutable set containing two elements.
static <E> Set<E> of(E e1, E e2, E e3) It It returns an immutable set containing three elements.
static <E> Set<E> of(E e1, E e2, E e3, E e4) It It returns an immutable set containing four elements.
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5) It It returns an immutable set containing five elements.
static <E> Set<E>
It It 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) It It 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) It It 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) It It 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) It It returns an immutable set containing ten elements.

You may also like