Java Collection Factory Methods: Simplifying Immutable Collection Creation

Discover the enhancements introduced in Java 9 that allow for easy creation of immutable collections using factory methods. This guide covers the new methods added to the List, Set, and Map interfaces, providing a concise and convenient way to create immutable instances. Streamline your code with these factory methods for efficient collection handling in Java.



Java - Collection Factory Methods

Factory Methods for Collection

In Java 9, collections were enhanced with new methods to create immutable lists in an easy and concise way. These factory methods were added to the List, Set, and Map interfaces to create immutable instances. These methods serve as convenience factory methods, offering a less verbose and more concise approach to creating collections.

Syntax

Before Java 9, the following syntax was used to create an immutable list:

List<String> unmodifiableList = Collections.unmodifiableList(arrayList);

Where arrayList is a mutable list instance. We first created a list, then used unmodifiableList() to get an immutable instance, preventing element modifications like addition or removal.

Factory Methods of List Interface

Starting from Java 9, the following methods can be used to create an immutable list:

static <E> List<E> of();
static <E> List<E> of(E e1);
static <E> List<E> of(E... elements);
static <E> List<E> of(E e1, E e2);
static <E> List<E> of(E e1, E e2, E e3);
static <E> List<E> of(E e1, E e2, E e3, E e4);
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5);
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6);
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7);
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8);
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9);
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);

Example of List Interface Factory Methods Before Java 9

Here is an example of how to create an unmodifiable list before Java 9:

package com.example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Tester {
public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    list.add("C");
    list = Collections.unmodifiableList(list);
    System.out.println(list);
}
}
[A, B, C]

Example of List Interface Factory Methods in Java 9

In Java 9, the factory methods make it much simpler:

package com.example;

import java.util.List;

public class Tester {
public static void main(String[] args) {
    List<String> list = List.of("A", "B", "C");
    System.out.println(list);
}
}
[A, B, C]

Factory Methods of Set Interface

Similarly, the Set interface has the following new methods to create an unmodifiable set:

static <E> Set<E> of();
static <E> Set<E> of(E e1);
static <E> Set<E> of(E... elements);
static <E> Set<E> of(E e1, E e2);
static <E> Set<E> of(E e1, E e2, E e3);
static <E> Set<E> of(E e1, E e2, E e3, E e4);
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5);
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6);
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7);
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8);
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9);
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);

Example of Set Interface Factory Methods Before Java 9

Here is an example of creating an unmodifiable set before Java 9:

package com.example;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Tester {
public static void main(String[] args) {
    Set<String> set = new HashSet<>();
    set.add("A");
    set.add("B");
    set.add("C");
    set = Collections.unmodifiableSet(set);
    System.out.println(set);
}
}
[A, B, C]

Example of Set Interface Factory Methods in Java 9

With the Java 9 factory methods, it becomes simpler:

package com.example;

import java.util.Set;

public class Tester {
public static void main(String[] args) {
    Set<String> set = Set.of("A", "B", "C");
    System.out.println(set);
}
}
[A, B, C]

Factory Methods of Map Interface

For the Map interface, the following methods are provided to create immutable maps. The ofEntries(...) method accepts varargs for multiple entries:

static <K,V> Map<K,V> of();
static <K,V> Map<K,V> of(K k1, V v1);
static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries);
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2);
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3);
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4);
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);
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);
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);
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);
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);
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);

Example of Map Interface Factory Methods Before Java 9

Here is an example of how to create an immutable map before Java 9:

package com.example;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Tester {
public static void main(String[] args) {
    Map<String, String> map = new HashMap<>();
    map.put("A", "Apple");
    map.put("B", "Banana");
    map.put("C", "Cherry");
    map = Collections.unmodifiableMap(map);
    System.out.println(map);
}
}
{A=Apple, B=Banana, C=Cherry}

Example of Map Interface Factory Methods in Java 9

With Java 9 factory methods, the code becomes simpler:

package com.example;

import java.util.Map;

public class Tester {
public static void main(String[] args) {
    Map<String, String> map = Map.of(
        "A", "Apple",
        "B", "Banana",
        "C", "Cherry"
    );
    System.out.println(map);
}
}
{A=Apple, B=Banana, C=Cherry}