☰ See All Chapters |
Immutable Collections
Immutable collections are used when non modifiable Collection instances are needed. Immutable List, Immutable Set and Immutable Map cannot be modified once instantiated. If we try to perform Add/Delete/Update operations on them, it throws UnsupportedOperationException at run time, but no errors/exception at compile time. Immutable List and Set don’t allow null elements. If we add null elements we get no error/exception during compile time but we get NullPointerException at run time. Immutable Map doesn’t allow null for both key and value. If we add null elements to either key or value of map we get no error/exception during compile time but we will get NullPointerException at run time.
Before java 9, to create immutable collection we used below static methods from Collections class.
Collections.unmodifiableList(elements)
Collections.unmodifiableSet(elements)
Collections.unmodifiableMap(elements)
package com.java4coding;
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 ImmutableCollectionBeforeJava9 {
public static void main(String[] args) { List<Integer> list = new ArrayList(); list.add(1); list.add(2); list.add(3); list = Collections.unmodifiableList(list);
Set<String> set = new HashSet(); set.add("One"); set.add("Two"); set.add("Three"); set = Collections.unmodifiableSet(set);
Map<Integer, String> map = new HashMap<>(); map.put(1, "One"); map.put(2, "Two"); map.put(3, "Three"); map = Collections.unmodifiableMap(map); } } |
This is a boring and repetitious work to add elements. To overcome these limitations Java 9 introduced Factory Methods for Immutable List, Set, Map. “of()” method is introduced in List, Set, to obtain the immutable instances.
How to create immutable List
Java 9 introduced below overloaded static factory methods in List class to create the immutable List.
static <E> List<E> of() {} static <E> List<E> of(E e1) {} 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) {} static <E> List<E> of(E... elements) {} |
Example
package com.java4coding;
import java.util.List;
public class ImmutableListInJava9 {
public static void main(String[] args) { List<Object> emptyImmuatableList = List.of(); List<String> stringImmuatableList = List.of("one", "two", "three"); List<Integer> integerImmuatableList = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); } } |
How to create immutable Set
Java 9 introduced below overloaded static factory methods in Set class to create the immutable Set.
static <E> Set<E> of() {} static <E> Set<E> of(E e1) {} 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) {} static <E> Set<E> of(E... elements) {} |
Example
package com.java4coding;
import java.util.Set;
public class ImmutableSetInJava9 {
public static void main(String[] args) { Set<Object> emptyImmuatableList = Set.of(); Set<String> stringImmuatableList = Set.of("one", "two", "three"); Set<Integer> integerImmuatableList = Set.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); } } |
How to create immutable Map
Java 9 introduced below overloaded static factory methods in Map class to create the immutable Map.
static <K, V> Map<K, V> of() {} static <K, V> Map<K, V> of(K k1, V v1) {} 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) {} static <K, V> Map<K, V> ofEntries(Entry<? extends K, ? extends V>... entries) {} |
Example
package com.java4coding; package com.java4coding;
import java.util.Map; import static java.util.Map.entry;
public class ImmutableMapInJava9 {
public static void main(String[] args) { Map<Object, Object> emptyImmuatableMap = Map.of(); Map<Integer, String> immuatableMap = Map.of(1, "one", 2, "two", 3, "Three"); Map<Integer, String> immuatableMapEntry = Map.ofEntries(entry(101, "PP"), entry(102, "QQ"), entry(103, "RR")); } } |
All Chapters