30% Therapy – 40% Practice – 30% Work project

Java – Collection Factory Methods



Factory Methods for Collection

In Java 9, were enhanced to have few new methods to create immutable list in an easy and concise way. These new factory methods were added to , , and interfaces to create immutable instances. These factory methods are mainly convenience factory methods in order to create a collection in less verbose and in concise way.

Syntax

Before Java 9, following syntax was used to create a immutable list.

List unmodifiableList = Collections.unmodifiableList(arrayList);

Where arrayList is a mutable list instance. So we are required to create a list and then using unmodifiableList() we get an immutable instance from which we cannot add/remove an element.

Factory Methods of List Interface

Now from Java 9, following methods can be used to create a 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);

Syntax

So from Java 9 onwards, following syntax can be used to create a immutable list. of(E… elements) method can be used to have more than 10 elements in an immutable list.

List<String> unmodifiableList = List.of("A", "B", "C");

Example of List Interface Factory Methods Before Java 9

Here, we are creating unmodifiable list before Java 9.

package com.tutorialspoint;

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);
   }  
}

Let us compile and run the above program, this will produce the following result −

[A, B, C]

Example of List Interface Factory Methods in Java 9

Here, we are creating unmodifiable list in Java 9.

package com.tutorialspoint;

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);
   }  
}

Let us compile and run the above program, this will produce the following result −

[A, B, C]

Factory Methods of Set Interface

On similar pattern, Set interface is having these new methods to create a unmodifiable Set to get an instance of a set from which we cannot add/remove an element.

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);

Syntax

So from Java 9 onwards, following syntax can be used to create a immutable set. of(E… elements) method can be used to have more than 10 elements in an immutable set.

Set<String> unmodifiableSet = Set.of("A", "B", "C");

Example of Set Interface Factory Methods Before Java 9

Here, we are creating unmodifiable set before Java 9.

package com.tutorialspoint;

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);
   }  
}

Let us compile and run the above program, this will produce the following result −

[A, B, C]

Example of Set Interface Factory Methods in Java 9

Here, we are creating unmodifiable set in Java 9.

package com.tutorialspoint;

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);
   }  
}

Let us compile and run the above program, this will produce the following result −

[A, B, C]

Factory Methods of Map Interface

In case of Map interface, we”ve ofEntries(…) can be used to accept var args parameter other than of() methods as shown below.

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 k1, V v10);

So from Java 9 onwards, following syntax can be used to create a immutable map.

Map<String, String> unmodifiableMap = Map.of("A","Apple", "B", "Boy", "C", "Cat");

Example of Map Interface Factory Methods Before Java 9

Here, we are creating unmodifiable Map before Java 9.

package com.tutorialspoint;

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","Boy");
      map.put("C","Cat");
      map = Collections.unmodifiableMap(map);
      System.out.println(map);
   }  
}

Let us compile and run the above program, this will produce the following result −

{A=Apple, B=Boy, C=Cat}

Example of Map Interface Factory Methods in Java 9

Here, we are creating unmodifiable Map in Java 9.

package com.tutorialspoint;

import java.util.Map;

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

Let us compile and run the above program, this will produce the following result −

{C=Cat, A=Apple, B=Boy}
Translate »