Collections
Hub › Java › Beginner › Collections
Goal
Build an immutable List of records with List.of(...) and iterate it. After this page you will have the exact data shape the exit endpoint returns.
Prerequisites
What a list is
A List<T> is an ordered collection of values of type T. The angle brackets are generics — they tell the compiler what the list holds. A List<Item> holds Item records and nothing else.
List.of(a, b, c) is the shortest way to build a list in modern Java. The list it returns is immutable — you cannot add to it, remove from it, or replace elements. For a fixed response payload that is what we want.
For mutable lists (where the contents change at runtime), Java uses ArrayList. We do not need it at this tier. For the full collections framework, see the Java Collections overview.
Code
import java.util.List;
public class ItemList {
record Item(int id, String name) {}
public static void main(String[] args) {
List<Item> items = List.of(
new Item(1, "notebook"),
new Item(2, "pen"),
new Item(3, "stapler")
);
for (var item : items) {
System.out.println(item.id() + ": " + item.name());
}
}
}Run it:
java ItemList.javaOutput:
1: notebook
2: pen
3: staplerWhat is new:
import java.util.List;— pullsListinto scope. Records andvarare in the default scope, butListis not.List<Item>— a list whose elements areItemrecords.for (var item : items)— the enhanced for loop. It walks every element of the list in order.
This is the data the exit endpoint will return as JSON.
Next → What Spring Boot is