Collections Of Objects
Real collections usually contain domain objects. A map can index those objects by a stable value such as an ISBN, making direct lookup possible.
The key does not need to be a field in the record, but it should identify the stored object clearly.
Group related objects
import java.util.LinkedHashMap;
import java.util.Map;
record Book(String title, String author) {}
public class Main {
public static void main(String[] args) {
Map<String, Book> catalog = new LinkedHashMap<>();
catalog.put("978-1", new Book("Dune", "Frank Herbert"));
catalog.put("978-2", new Book("Hamlet", "Shakespeare"));
Book found = catalog.get("978-1");
System.out.println(found.title());
System.out.println(found.author());
}
}
Output:
Dune
Frank Herbert
The map stores complete Book objects rather than isolated titles. Looking up one ISBN returns the record, whose accessors expose its fields.
Choose deterministic map implementations when output order matters. A plain HashMap deliberately makes no iteration-order promise.
Now combine a map and lists to group song titles by artist.