Java / Java 21 Collection Framework features Interview questions
What are pollFirstEntry() and pollLastEntry()?
pollFirstEntry() and pollLastEntry() remove and return the first and last entries of a SequencedMap, combining a read and a delete into one call.
LinkedHashMap<String, Integer> queue = new LinkedHashMap<>(); queue.put("job1", 1); queue.put("job2", 2); Map.Entry<String, Integer> next = queue.pollFirstEntry(); // job1=1, removed
Unlike firstEntry()/lastEntry(), these mutate the map. They also return null rather than throwing when the map is empty, which makes them convenient for drain-until-empty loops.
More Related questions...