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 List |
Of() |
It It returns an immutable list containing zero elements. |
static List |
of(E e1) |
It It returns an immutable list containing one element. |
static List |
of(E... elements) |
It It returns an immutable list containing an arbitrary number of elements. |
static List |
of(E e1, E e2) |
It It returns an immutable list containing two elements. |
static List |
of(E e1, E e2, E e3) |
It It returns an immutable list containing three elements. |
static List |
of(E e1, E e2, E e3, E e4) |
It It returns an immutable list containing four elements. |
static List |
of(E e1, E e2, E e3, E e4, E e5) |
It It returns an immutable list containing five elements. |
static List |
of(E e1, E e2, E e3, E e4, E e5, E e6) |
It It returns an immutable list containing six elements. |
static List |
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 List |
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 List |
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 List |
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.
Modifier and Type |
Method |
Description |
static Set |
of() |
It It returns an immutable set containing zero elements. |
static Set |
of(E e1) |
It It returns an immutable set containing one element. |
static Set |
of(E... elements) |
It It returns an immutable set containing an arbitrary number of elements. |
static Set |
of(E e1, E e2) |
It It returns an immutable set containing two elements. |
static Set |
of(E e1, E e2, E e3) |
It It returns an immutable set containing three elements. |
static Set |
of(E e1, E e2, E e3, E e4) |
It It returns an immutable set containing four elements. |
static Set |
of(E e1, E e2, E e3, E e4, E e5) |
It It returns an immutable set containing five elements. |
static Set |
It It returns an immutable set containing six elements. |
static Set |
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 Set |
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 Set |
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 Set |
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. |
Java 9 Set Interface Factory Methods Example
Output:
Spring
JavaFX
JSP
Java
Hibernate
Java 9 Map Interface Factory Methods In Java 9, Map includes Map.of() and Map.ofEntries() static factory methods that provide a convenient way to creae immutable maps. Map created by these methods has the following characteristics.
- It is immutable
- It does not allow null keys and values
- It is serializable if all keys and values are serializable
- It rejects duplicate keys at creation time
- The iteration order of mappings is unspecified and is subject to change.
Java 9 Map Interface Factory Methods The following table contains the factory methods for Map interface.
Modifier and Type |
Method |
Description |
static Map |
of() |
It returns an immutable map containing zero mappings. |
static Map |
of(K k1, V v1) |
It returns an immutable map containing a single mapping. |
static Map |
of(K k1, V v1, K k2, V v2) |
It returns an immutable map containing two mappings. |
static Map |
of(K k1, V v1, K k2, V v2, K k3, V v3) |
It returns an immutable map containing three mappings. |
static Map |
of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) |
It returns an immutable map containing four mappings. |
static Map |
of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) |
It returns an immutable map containing five mappings. |
static Map |
of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) |
It returns an immutable map containing six mappings. |
static Map |
of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) |
It returns an immutable map containing seven mappings. |
static Map |
of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) |
It returns an immutable map containing eight mappings. |
static Map |
of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) |
It returns an immutable map containing nine mappings. |
static Map |
of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) |
It returns an immutable map containing ten mappings. |
static Map |
ofEntries(Map.Entry... entries) |
It returns an immutable map containing keys and values extracted from the given entries. |
Java 9 Map Interface Factory Methods Example
Output:
102 Hibernate
103 Spring MVC
101 JavaFX
Java 9 Map Interface ofEntries() Method Example In Java 9, apart from static Map.of() methods, Map interface includes one more static method Map.ofEntries(). This method is used to create a map of Map.Entry instances. In the following example, we are creating map instance with the help of multiple map.entry instances.
Output:
|