How to initialize immutable collections in Java 9?



Java 9 provides factory methods to create immutable lists, sets, and maps. It can be useful to create empty or non-empty collection objects. In Java 8 and earlier versions, we can use collection class utility methods like unmodifiableXXX to create immutable collection objects. If we need to create an immutable list, then use the Collections.unmodifiableList() method.

What does Immutable mean?

An object is considered immutable if its state cannot change after it is constructed. Any modification creates a new object instead.

String name = "TutorialsPoint";
name = name + "10";

Here, "TutorialsPoint" is an immutable string, and we cannot modify it directly. Instead, we use the addition operator to make changes, which will result in "TutorialsPoint10" as the new string.

What is an Immutable Collection?

Immutable collections in Java are those collections that can't be modified once they are created. Any attempt to add, set, or remove elements from these collections causes an UnsupportedOperationException to be thrown.

They are thread-safe because no thread can modify them as the structure doesn't support mutation. Java 9 provides List.of(), Set.of(), and Map.of() for creating immutable collections in Java.

List<String> colors = List.of("Apple", "Orange");
colors.add("Orange");

Here, colors is an immutable list, and trying to add "Orange" to the list will throw an UnsupportedOperationException error.

Immutable Collections in Java

The following are the immutable Collections in Java:

  • List
  • Set
  • Map

Immutable List

In Java, a List is an ordered collection of elements that can contain duplicates. It is an interface that extends the Collection interface and offers methods to add, delete, access, and manipulate elements in a sequence.

An immutable list is a fixed-size collection where entries cannot be added, removed, or modified after the list's creation, maintaining thread safety and preventing accidental changes.

Initialization of an immutable list using the unmodifiableList() method:

List<Integer> unmodifiableList = Collections.unmodifiableList(muttablelist);

Initialization of an immutable list using the List.of() method:

List<Integer> immutableEmptyList = List.of();

Example

Below is an example of creating an immutable List in Java:

import java.util.*;
public class ImmutableListExample {
    public static void main(String[] args) {
        List list8= new ArrayList<>();
        list8.add("INDIA");
        list8.add("AUSTRALIA");
        list8.add("ENGLAND");
        list8.add("NEWZEALAND");
        //using the unmodifiableList() method
        List immutableList8 = Collections.unmodifiableList(list8);
        immutableList8.forEach(System.out::println);
        System.out.println();
        //using the List.of() method
        List immutableList = List.of("INDIA", "AUSTRALIA", "ENGLAND", "NEWZEALAND");
        immutableList.forEach(System.out::println);
    }
}

Output

INDIA
AUSTRALIA
ENGLAND
NEWZEALAND

INDIA
AUSTRALIA
ENGLAND
NEWZEALAND

Immutable Set

Set is an interface that inherits the Collection interface and contains no duplicate elements. A Set interface has methods to add, delete, and check the existence of an element. Some of the implementations of the Set interface are HashSet, TreeSet, and LinkedHashSet

An immutable set is an unchangeable collection of unique elements that doesn't allow any modification in the set.

Initialization of an immutable Set using the unmodifiableSet() method:

Set<Integer> unmodifiableSet = Collections.unmodifiableSet(mutableSet);

Initialization of an immutable Set using the Set.of() method:

Set<Integer> immutableEmptySet = Set.of();

Example

Below is an example of creating an immutable Set in Java:

import java.util.*;
public class ImmutableSetExample {
    public static void main(String[] args) {
        Set<String> Set8 = new HashSet<>();
        Set8.add("INDIA");
        Set8.add("AUSTRALIA");
        Set8.add("ENGLAND");
        Set8.add("NEWZEALAND");
        //using the unmodifiableSet() method
        Set immutableSet8 = Collections.unmodifiableSet(Set8);
        immutableSet8.forEach(System.out::println);
        System.out.println();
        //using the Set.of() method
        Set<String> immutableSet = Set.of("INDIA", "AUSTRALIA", "ENGLAND", "NEWZEALAND");
        immutableSet.forEach(System.out::println);
    }
}

Output

INDIA
AUSTRALIA
ENGLAND
NEWZEALAND

INDIA
AUSTRALIA
ENGLAND
NEWZEALAND

Immutable Map

In Java, a Map is an interface in the Collections framework used to store data in the form of key-value pairs. Each key in the map is unique and maps to some value. Some of the implementations of the Set interface are HashMapTreeMap, and LinkedHashMap.

An immutable map is a key-value store that blocks all put/remove operations after creating an immutable Map interface.

Map

Initialization of an immutable Map using the unmodifiableMap() method:

List<Integer> unmodifiableList = Collections.unmodifiableMap(muttablelist);

Initialization of an immutable Map using the Map.of() method:

Map<Integer, Integer> immutableEmptyMap = Map.of();

Example

Below is an example of creating an immutable Set in Java:

import java.util.*;
public class ImmutableMapExample {
    public static void main(String[] args) {
        Map<String, String> Map8 = new HashMap<>();
        Map8.put("INDIA", "India");
        Map8.put("AUSTRALIA", "Australia");
        Map8.put("ENGLAND", "England");
        Map8.put("NEWZEALAND", "Newzealand");
        //using the unmodifiableMap() method
        Map<String, String> immutableMap8 = Collections.unmodifiableMap(Map8);
        immutableMap8.forEach((key, value) -> System.out.println(key + " : " + value));
        System.out.println();
        //using the Map.of() method
        Map<String, String> immutableMap = Map.of(
            "INDIA", "India",
            "AUSTRALIA", "Australia",
            "ENGLAND", "England",
            "NEWZEALAND", "Newzealand"
        );
        immutableMap.forEach((key, value) -> System.out.println(key + " : " + value));
    }
}

Output

AUSTRALIA : Australia
ENGLAND : England
INDIA : India
NEWZEALAND : Newzealand

NEWZEALAND : Newzealand
ENGLAND : England
AUSTRALIA : Australia
INDIA : India
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-06-10T18:40:27+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements