Java / Java 21 Collection Framework features Interview questions
What are firstEntry() and lastEntry() in SequencedMap?
firstEntry() and lastEntry() return the first and last key-value pairs of a SequencedMap, wrapped as Map.Entry objects, without removing them from the map.
LinkedHashMap<String, Integer> scores = new LinkedHashMap<>(); scores.put("Ann", 90); scores.put("Ben", 85); System.out.println(scores.firstEntry()); // Ann=90
They return null if the map is empty, unlike getFirst() on a SequencedCollection, which throws an exception - a small but important inconsistency to remember when writing null-safe code.
More Related questions...