Java / Java 21 Collection Framework features Interview questions
What are putFirst() and putLast() in SequencedMap?
putFirst(K, V) inserts or moves a key-value pair to the front of a SequencedMap's encounter order, and putLast(K, V) does the same at the end.
If the key already exists, calling either method removes it from its current position and reinserts it at the requested end, updating the value - this makes them useful for building recency-ordered structures like a simple LRU cache.
SequencedMap<String, Integer> map = new LinkedHashMap<>(); map.put("a", 1); map.put("b", 2); map.putFirst("c", 3); // c is now first
Both throw UnsupportedOperationException on an unmodifiable map, since they change the map's contents.
More Related questions...