In this article, we are going to discuss about static factory methods that Java 9 brought to the Collections library. These new methods are added to List, Set and Map to create immutable collection instances. These methods are also referred to as convenience factory methods because it helps to create collection in simpler & compact way.
Check out: Iterate / ofNullable operations in Java 9 Stream
Simpler way of Creating Collections in Java 9...!!! Click To Tweet
In this article, we are going to cover below points:
- What are Factory methods for Collections?
- Complete List of Factory methods for List, Set & Map Interface.
- Old & New way of creating List, Set & Map with example in Java
Let’s begin:
1. What are Factory methods for Collections?
Factory methods are static methods used to create immutable instances of collections. It is unmodifiable, therefore adding new element will throw java.lang.UnsupportedOperationException
2. List, Set & Map Interface Factory Methods
LIST Interface Factory Methods
static <E> List<E> | of() | Returns an immutable list containing zero elements. |
---|---|---|
static <E> List<E> | of(E e1) | Returns an immutable list containing one element. |
static <E> List<E> | of(E... elements) | Returns an immutable list containing an arbitrary number of elements. |
static <E> List<E> | of(E e1,E e2) | Returns an immutable list containing two elements. |
static <E> List<E> | of(E e1,E e2,E e3) | Returns an immutable list containing three elements. |
static <E> List<E> | of(E e1,E e2,E e3,E e4) | Returns an immutable list containing four elements. |
static <E> List<E> | of(E e1,E e2,E e3,E e4,E e5) | Returns an immutable list containing five elements. |
static <E> List<E> | of(E e1,E e2,E e3,E e4,E e5,E e6) | 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) | 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) | 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) | 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) | Returns an immutable list containing ten elements. |
SET Interface Factory Methods
static <E> Set<E> | of() | Returns an immutable set containing zero elements. |
---|---|---|
static <E> Set<E> | of(E e1) | Returns an immutable set containing one element. |
static <E> Set<E> | of(E... elements) | Returns an immutable set containing an arbitrary number of elements. |
static <E> Set<E> | of(E e1,E e2) | Returns an immutable set containing two elements. |
static <E> Set<E> | of(E e1,E e2,E e3) | Returns an immutable set containing three elements. |
static <E> Set<E> | of(E e1,E e2,E e3,E e4) | Returns an immutable set containing four elements. |
static <E> Set<E> | of(E e1,E e2,E e3,E e4,E e5) | Returns an immutable set containing five elements. |
static <E> Set<E> | of(E e1,E e2,E e3,E e4,E e5,E e6) | 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) | 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) | 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) | 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) | Returns an immutable set containing ten elements. |
MAP Interface Factory Methods
static <K,V> Map.Entry<K,V> | entry(K k,V v) | Returns an immutable Map.Entry containing the given key and value. |
---|---|---|
static <K,V> Map<K,V> | of() | Returns an immutable map containing zero mappings. |
static <K,V> Map<K,V> | of(K k1,V v1) | Returns an immutable map containing a single mapping. |
static <K,V> Map<K,V> | of(K k1,V v1,K k2,V v2) | 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) | 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) | 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) | 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) | 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) | 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) | 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) | 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) | Returns an immutable map containing ten mappings. |
static <K,V> Map<K,V> | ofEntries(Map.Entry<? extends K,? extends V>... entries) | Returns an immutable map containing keys and values extracted from the given entries. |
3. Old Vs New way of Creating List, Set & Map
3.1 Creating LIST
OLD Way (Before Java 9)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Creating_List_Old_Way_Before_Java9_Example { public static void main(String args[]) { //Before Java 9 - Creating List List <String> list = new ArrayList <> (); list.add("Fruit"); list.add("Car"); list.add("Animal"); list = Collections.unmodifiableList(list); System.out.println("List values are: " + list); } } |
NEW Way (Since Java 9) Immutable List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.List; public class Creating_List_New_Way_Java9_Example { public static void main(String[] args) { //Java 9 - Creating List List <String> list = List.of("Fruit", "Car", "Animal"); //Java 7 - Printing Set System.out.println("Printing list using traditional for loop:\n"); for (String s: list) { System.out.println(s); } //Java 8 - Printing Set System.out.println("\nPrinting list using forEach:\n"); list.stream().forEach(s -> System.out.println(s)); } } |
OLD Way (Before Java 9)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.Collections; import java.util.HashSet; import java.util.Set; public class Creating_Set_Old_Way_Before_Java9_Example { public static void main(String args[]) { //Before Java 9 - Creating Set Set <String> set = new HashSet <> (); set.add("Fruit"); set.add("Car"); set.add("Animal"); set = Collections.unmodifiableSet(set); System.out.println("Set values are: " + set); } } |
NEW Way (Since Java 9) Immutable Set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.Set; public class Creating_Set_New_Way_Java9_Example { public static void main(String[] args) { //Java 9 - Creating Set Set <String> set = Set.of("Fruit", "Car", "Animal"); //Java 7 - Printing Set System.out.println("Printing set using traditional for loop:\n"); for (String s: set) { System.out.println(s); } //Java 8 - Printing Set System.out.println("\nPrinting set using forEach:\n"); set.stream().forEach(s -> System.out.println(s)); } } |
OLD Way (Before Java 9)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.Collections; import java.util.HashMap; import java.util.Map; public class Creating_Map_Old_Way_Before_Java9_Example { public static void main(String args[]) { //Before Java 9 - Creating Map Map <String, String> map = new HashMap <> (); map.put("Fruit", "Mango"); map.put("Car", "Mercedes"); map.put("Animal", "Dog"); map = Collections.unmodifiableMap(map); System.out.println("Map values are: " + map); } } |
First NEW Way (Since Java 9) Immutable Map
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.Map; public class Creating_Map_New_Way_Java9_Example1 { public static void main(String[] args) { //Java 9 - Creating Map Map <String, String> map = Map.of("Fruit", "Mango", "Car", "Mercedes", "Animal", "Dog"); //Java 7 - Printing Map System.out.println("Print map using traditional for loop:\n"); for (Map.Entry <String, String> mapObj: map.entrySet()) { System.out.println(mapObj.getKey() + " : " + mapObj.getValue()); } //Java 8 - Printing Map System.out.println("\nPrint map using forEach:\n"); map.forEach((k, v) -> System.out.println(k + " : " + v)); } } |
Second NEW Way (Since Java 9) Immutable Map
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.AbstractMap; import java.util.Map; public class Creating_Map_New_Way_Java9_Example2 { public static void main(String[] args) { //Java 9 - Creating Map Map <String, String> map = Map.ofEntries( new AbstractMap.SimpleEntry <> ("Fruit", "Mango"), new AbstractMap.SimpleEntry <> ("Car", "Mercedes"), new AbstractMap.SimpleEntry <> ("Animal", "Dog")); //Java 7 - Printing Map System.out.println("Printing map using tradition for loop:\n"); for (Map.Entry <String, String> mapObj: map.entrySet()) { System.out.println(mapObj.getKey() + " : " + mapObj.getValue()); } //Java 8 - Printing Map System.out.println("\nPrinting map using forEach:\n"); map.forEach((k, v) -> System.out.println(k + " : " + v)); } } |
Simpler way of Creating Collections in Java 9...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- dropWhile / takeWhile operations in Java 9 Streams
- Collection sorting using Lambda in Java 8
- Iterate / ofNullable operations in Java 9 Streams