English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Other Java topics

Java 9 Collection factory methods

Java 9 New Features

Java 9 New static factory methods can create immutable instances of these collections in List, Set, and Map interfaces.

These factory methods can create collections in a more concise way.

Old method to create a collection

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
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);
      List<String> list = new ArrayList<>();
 
      list.add("A");
      list.add("B");
      list.add("C");
      list = Collections.unmodifiableList(list);
      System.out.println(list);
      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);
   }
}

The execution output is as follows:

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

New method to create a collection

Java 9 In which, the following methods are added to the List, Set and Map interfaces and their overloaded objects.

static <E> List<E> of(E e1, E e2, E e3);
static <E> Set<E> of(E e1, E e2, E e3);
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> ofEntries(Map.Entry<? extends K,? extends V>... entries)
  • The of(...) method of the List and Set interfaces overloads 0 ~ 10 parameters.

  • The of(...) method of the Map interface overloads 0 ~ 10 parameters.

  • Different methods of the Map interface if more than 10 parameter, you can use the ofEntries(...) method.

New method to create a collection

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.AbstractMap;
import java.util.Map;
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);
      List<String> list = List.of(
      System.out.println(list);
      Map<String, String> map = Map.of(
      System.out.println(map);
  
      Map<String, String> map1 = Map.ofEntries (
         
         new AbstractMap.SimpleEntry<>(
         new AbstractMap.SimpleEntry<>(
      System.out.println(map1);
   }
}

The output result is:

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

Java 9 New Features