Java 21 gave LinkedHashMap the SequencedMap interface, so putFirst, putLast, firstEntry and reversed are available without copying the map.
SequencedMap<String, Integer> steps = new LinkedHashMap<>();
steps.put("build", 2);
steps.put("test", 3);
steps.putFirst("checkout", 1);
steps.putLast("deploy", 4);
System.out.println(steps);
System.out.println("firstEntry = " + steps.firstEntry());
System.out.println("lastEntry = " + steps.lastEntry());
System.out.println("reversed = " + steps.reversed());
{checkout=1, build=2, test=3, deploy=4}
firstEntry = checkout=1
lastEntry = deploy=4
reversed = {deploy=4, test=3, build=2, checkout=1}
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-30