Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How Do You Create Immutable Collections with List.of and Map.of in Java?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 6, 2026
6 min read

Call List.of(...), Set.of(...) or Map.of(...) to create an immutable collection in one line. The result cannot be changed: add, put, remove and clear throw UnsupportedOperationException, and null elements are rejected. Use List.copyOf, Set.copyOf and Map.copyOf to take an unmodifiable snapshot of an existing collection.

These immutable collections list.of factory methods arrived in Java 9 and are the standard way to write constants, test data and return values that callers must not modify.

Table of Contents

  • Creating immutable collections with the of factory methods
  • What happens when you try to modify them
  • Rules and restrictions
  • Larger maps with Map.ofEntries
  • Copying with copyOf versus unmodifiableList
  • Using immutable collections in real code
  • Frequently Asked Questions
    • Is List.of the same as Arrays.asList?
    • Which Java version added List.of and Map.of?
    • Can I add null to an immutable list?
    • Are these collections thread safe?
  • Summary
  • Related Articles

Creating immutable collections with the of factory methods

Each interface has static of methods that take the elements directly.

java
text

List.of() with no arguments gives an empty immutable list. Overloads take up to ten elements, and a varargs overload accepts any number beyond that. Map.of takes up to ten key and value pairs.

What happens when you try to modify them

Mutating methods always throw, even when they would not change anything.

java
text

The exception is thrown at the call, not at creation, so a mistaken add shows up as a run time failure. Treat it as a design signal: if code needs to change the collection, it should build a new one.

Rules and restrictions

The factory methods are strict by design.

RuleBehaviour
Null elements, keys or valuesNullPointerException at creation
Set.of with duplicate elementsIllegalArgumentException
Map.of with duplicate keysIllegalArgumentException
contains(null) on the resultNullPointerException for List.of and Set.of
Iteration order of Set.of and Map.ofUnspecified, and can differ between JVM runs
java
text

Never rely on the printed order of a Set.of or Map.of result. The order is deliberately randomised per JVM instance so that code does not depend on it. If you need a stable order, use List.of, or wrap a LinkedHashMap or LinkedHashSet with Collections.unmodifiableMap or Collections.unmodifiableSet.

Larger maps with Map.ofEntries

Map.of stops at ten pairs. For more, or for readability, use Map.entry with Map.ofEntries.

java
text

Copying with copyOf versus unmodifiableList

List.copyOf(source) makes an independent immutable copy. Collections.unmodifiableList(source) only wraps the original in a read only view, so later changes to the source show through the view.

java
text

copyOf also avoids copying when the argument is already an immutable collection from the of family, so it is cheap to call defensively. Streams offer the same result: Stream.toList() (Java 16+) returns an unmodifiable list, though unlike List.of it allows null elements.

Using immutable collections in real code

Immutable collections are best used for:

  1. Constants: private static final List<String> ROLES = List.of("admin", "user");
  2. Return values: return List.copyOf(items) from a getter so callers cannot change internal state.
  3. Records: copy mutable components in the compact constructor.
java

An important caveat: the collection is immutable, but the elements may not be. A List.of(new StringBuilder()) still lets you change the builder. For true immutability, store immutable elements such as strings, records of immutable fields, or java.time values.

Frequently Asked Questions

Is List.of the same as Arrays.asList?

No. Arrays.asList returns a fixed size list that still allows set and writes through to the array. List.of is fully immutable and rejects nulls.

Which Java version added List.of and Map.of?

Java 9 added them. copyOf methods came in Java 10, and Stream.toList() in Java 16.

Can I add null to an immutable list?

Not with List.of, Set.of or Map.of; they throw NullPointerException. Use Collections.unmodifiableList over a normal list if you really need nulls.

Are these collections thread safe?

Yes for sharing. Because their contents cannot change after creation, any number of threads can read them without locking, provided the elements are themselves safe to share.

Summary

Use List.of, Set.of and Map.of (or Map.ofEntries) to build immutable collections, and copyOf to snapshot existing ones. They reject nulls and duplicates, throw UnsupportedOperationException on mutation, and give no guarantee about Set or Map order. Remember that only the collection is frozen, not the objects inside it.

Related Articles

  • What is the purpose of the Collections utility class in Java?
  • Which Java Collection Should You Choose? A Practical Complexity Guide
  • Learn Java: Complete Study Roadmap from Beginner to Advanced
Share

Similar posts

  • What Is the Difference Between Comparable and Comparator in Java?
  • What Is the Fail-Fast Behavior and ConcurrentModificationException in Java?
  • What Are Queue and Stack in Java? ArrayDeque vs Stack Class
  • How Do You Remove Duplicates from a List in Java?
  • How Do You Use the Arrays Class for Sorting and Searching in Java?
  • How Do You Iterate Over a Map in Java? Four Ways with Examples
  • How Does HashMap Work Internally in Java? Buckets and Hashing
  • What Is the Difference Between ArrayList and LinkedList in Java?