Java 21: SequencedMap first/last entries

LinkedHashMap now exposes firstEntry, lastEntry and reversed views.

Code
LinkedHashMap<String, Integer> m = new LinkedHashMap<>();
m.put("a", 1); m.put("b", 2); m.put("c", 3);
System.out.println(m.firstEntry());
System.out.println(m.lastEntry());
Output
a=1
c=3
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-26