Choosing a Java Collection: Immutable and Sequenced Collections
A key to look up? Each value once? A line? Otherwise a List. Then the locks: List.of and Map.of refuse Chai, an unmodifiable view follows its owner to [Latte, Mocha] while a copyOf snapshot stays at [Latte], and Java 21 adds getFirst, getLast and reversed.
What you will learn
- Pick List, Set, Queue or Map by asking a few questions in order
- Build fixed collections with List.of and Map.of and know what they refuse
- Tell a read-only view from a copy made with List.copyOf
- Use the Java 21 methods getFirst, getLast and reversed
Before you start
- Java ArrayList
- Java maps: HashMap, LinkedHashMap and TreeMap
Picking a collection is like picking a bag: a backpack, a suitcase or a wallet. Sometimes the bag also needs a lock so nothing inside can change. This lesson covers both choices.
Ask the questions from the top
List<String> orders = new ArrayList<>(); // keeps order
orders.add("Latte");
orders.add("Mocha");
orders.add("Latte"); // a repeat
Set<String> drinks = new TreeSet<>(orders); // each once
Map<String, Integer> prices = new HashMap<>(); // by key
prices.put("Latte", 4);
Queue<String> pickup = new ArrayDeque<>(orders); // FIFO
System.out.println("List: " + orders);
System.out.println("Set: " + drinks);
System.out.println("Map: " + prices.get("Latte"));
System.out.println("Queue: " + pickup.poll());
List: [Latte, Mocha, Latte]
Set: [Latte, Mocha]
Map: 4
Queue: Latte
A quick refresher: the list keeps the repeat and the order, the TreeSet drops the second Latte, the map finds 4 by key, and the queue's poll hands back the first order. A Queue has no index, so pickup.get(0) doesn't compile.
A menu that cannot change
List<String> menu = List.of("Latte", "Mocha");
Map<String, Integer> prices = Map.of("Latte", 4, "Mocha", 5);
System.out.println("Menu: " + menu);
System.out.println("Mocha: " + prices.get("Mocha"));
try {
menu.add("Chai"); // refused
} catch (UnsupportedOperationException e) {
System.out.println("add: " + e);
}
try {
prices.put("Chai", 3); // refused
} catch (UnsupportedOperationException e) {
System.out.println("put: " + e);
}
Menu: [Latte, Mocha]
Mocha: 5
add: java.lang.UnsupportedOperationException
put: java.lang.UnsupportedOperationException
One line builds each collection, and reading works as usual. But the lock goes on right away: add and put throw UnsupportedOperationException, and the menu stays the same. List.of doesn't accept null either; List.of("Latte", null) throws a NullPointerException.
A window, or a snapshot
List<String> orders = new ArrayList<>();
orders.add("Latte");
List<String> view = Collections.unmodifiableList(orders);
List<String> copy = List.copyOf(orders);
try {
view.add("Chai"); // callers: blocked
} catch (UnsupportedOperationException e) {
System.out.println("Blocked: " + e);
}
System.out.println("Before: view " + view + ", copy " + copy);
orders.add("Mocha"); // the owner adds
System.out.println("After: view " + view + ", copy " + copy);
Blocked: java.lang.UnsupportedOperationException
Before: view [Latte], copy [Latte]
After: view [Latte, Mocha], copy [Latte]
Say you pass a list to a method that shouldn't change it. Collections.unmodifiableList gives a view: a glass window on orders. You can look through it but not reach in, so view.add is blocked. List.copyOf gives a copy, a snapshot taken right now. The owner can still change orders; when it adds Mocha, the view shows it, while the copy still shows only Latte. The copy is locked too: copy.add("Chai") throws the same exception.
First, last and backwards (Java 21)
List<String> orders = List.of("Latte", "Mocha", "Chai");
// before Java 21
String lastOld = orders.get(orders.size() - 1);
System.out.println("Old way: " + lastOld);
// Java 21 and later
System.out.println("First: " + orders.getFirst());
System.out.println("Last: " + orders.getLast());
System.out.println("Reversed: " + orders.reversed());
Old way: Chai
First: Latte
Last: Chai
Reversed: [Chai, Mocha, Latte]
get(size() - 1) finds Chai, but it's clumsy. Since Java 21, getFirst and getLast read the two ends and reversed shows the same items backwards. This list comes from List.of, so it's still locked: orders.removeFirst() throws UnsupportedOperationException.
Locked, watched or open
List.of / copyOf | unmodifiable view | ArrayList | |
|---|---|---|---|
| Can a caller add? | no | no | yes |
| After it's made | never changes | follows the owner | changes when you edit it |
| Use it for | a fixed menu, a snapshot | a read-only hand-out | a list you must edit |
That wraps up collections. Java 21 to 25 brought more changes like these; see what's new in Java 21 to 25.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Java equals and hashCode: Why a HashSet Needs Both
== asks if it's the very same object, equals compares values, and hashCode puts equal objects in the same HashSet bucket. Three small programs show what breaks when hashCode is missing.
From Arrays to Collections: The Java Collections Framework Map
An array of 3 students can't seat John, so Java throws Index 3 out of bounds. A List just grows. Then the map of the framework: List, Set and Queue under Collection, Map beside it, four behaviours in one program, and why the variable type should be the interface.
Java Wrapper Classes and Autoboxing
List<int> won't compile because a list holds objects only. Integer.valueOf puts 87 in a box and intValue takes it out, and add and get do it for you. Then two traps: 128 == 128 is false, and unboxing a null Integer throws a NullPointerException.
Java ArrayList: The Stretchy Array
add, add at an index, get, set, remove and contains on a list of courses and a cart, why Git at index 1 pushes SQL and Spring right, removing scores below 50 with an Iterator or removeIf, List.of that can't change, and the array inside that grows from 10 to 15.
