New & Simpler way of creating Collections in Java 9

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:

  1. What are Factory methods for Collections?
  2. Complete List of Factory methods for List, Set & Map Interface. 
  3. 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)

 

NEW Way (Since Java 9) Immutable List

 

3.1 Creating SET

OLD Way (Before Java 9)

 

NEW Way (Since Java 9) Immutable Set

 

3.1 Creating MAP

OLD Way (Before Java 9)

 

First NEW Way (Since Java 9) Immutable Map

 

Second NEW Way (Since Java 9) Immutable Map

 

Creating Immutable Collection is literally a very useful addition as part of Java 9. I hope above examples could help you to get better idea on how to implement it.

Simpler way of Creating Collections in Java 9...!!! Click To Tweet

Do you like this Post? – then check my other helpful posts:

Other Useful References:

 

Related Posts:

[wcp-carousel id=”5342″]

Promotional Content:

Loading RSS Feed

Leave a Reply

Your email address will not be published.Required fields are marked *

Facebook Twitter Instagram YouTube