Collection Factory Methods

Java added some factory methods to the List, Set, and Map interface in Java 9 version. These methods are useful to create an immutable collection with a simple and concise code.

List interface Factory Methods

Methods
Description
static <E> List<E>Of() It returns an immutable list containing zero elements.
static <E> List<E>of(E e1) It returns an immutable list containing one element.
static <E> List<E>of(E... elements) It returns an immutable list containing an arbitrary number of elements.
static <E> List<E>of(E e1, E e2) It returns an immutable list containing two elements.
static <E> List<E>of(E e1, E e2, E e3) It returns an immutable list containing three elements.
static <E> List<E>of(E e1, E e2, E e3, E e4) It returns an immutable list containing four elements.
static <E> List<E>of(E e1, E e2, E e3, E e4, E e5) 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 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 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 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 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 returns an immutable list containing ten elements.

Example: List

In this example, we are creating an immutable list by using the factory method of List interface.

import java.util.List;

public class Main { 
	public static void main(String[] args){  
        List<String> fruits = List.of("Orange","Mango","Apple");
        System.out.println(fruits);
        // Traversing using for-each
        for (String fruit : fruits) {
			System.out.println(fruit);
		}
	}
}


[Orange, Mango, Apple]
Orange
Mango
Apple

Set interface Factory Methods

Like the List interface, the Set interface also contains factory methods to create an immutable set. The following is the table of factory methods of the Set interface.

Modifier and Type 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>of(E e1, E e2, E e3, E e4, E e5, E e6) 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.

Example: Set

In this example, we are using the factory method of Set interface to create an immutable set.

public class Main { 
	public static void main(String[] args){  
        Set<String> fruits = Set.of("Orange","Mango","Apple");
        System.out.println(fruits);
        // Traversing using for-each
        for (String fruit : fruits) {
			System.out.println(fruit);
		}
	}
}


[Orange, Mango, Apple]
Orange
Mango
Apple

Map Interface Factory Methods

The following is a table of Map interface factory methods.

Method Description
static <K,V> Map<K,V>of() It returns an immutable map containing zero mappings.
static <K,V> Map<K,V>of(K k1, V v1) It returns an immutable map containing a single mapping.
static <K,V> Map<K,V>of(K k1, V v1, K k2, V v2) It returns an immutable map containing two mappings.
static <K,V> Map<K,V>of(K k1, V v1, K k2, V v2, K k3, V v3) It returns an immutable map containing three mappings.
static <K,V> Map<K,V>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 <K,V> Map<K,V>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 <K,V> Map<K,V>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 <K,V> Map<K,V> 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 <K,V> Map<K,V> 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 <K,V> Map<K,V> 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 <K,V> Map<K,V> 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 <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries) It returns an immutable map containing keys and values extracted from the given entries.

Example: Map

In this example, we are creating an immutable map by using the factory method.

import java.util.Map;

public class Main { 
	public static void main(String[] args){  
        Map<Integer,String> fruits = Map.of(101,"Orange",102,"Mango",103,"Apple");
        System.out.println(fruits);
        // Traversing using for-each
        for(Map.Entry<Integer, String> m : fruits.entrySet()){    
			System.out.println(m.getKey()+" "+m.getValue());
		}
	}
}


{102=Mango, 101=Orange, 103=Apple}
102 Mango
101 Orange
103 Apple

Example 2: Map Factory Method

import java.util.Map;

public class Main { 
	public static void main(String[] args){  
		Map<Integer, String> fruits = Map.ofEntries(
                Map.entry(101,"Orange"),
                Map.entry(102, "Apple"),
                Map.entry(103, "Mango"));
        System.out.println(fruits);
        // Traversing using for-each
        for(Map.Entry<Integer, String> m : fruits.entrySet()){    
			System.out.println(m.getKey()+" "+m.getValue());
		}
	}
}


{102=Apple, 103=Mango, 101=Orange}
102 Apple
103 Mango
101 Orange