MediumExperience level: 3-5 years · 8-15 years2 min read

Java 21 Sequenced Collections — Interview Questions

Reviewed by Gurusankar M.

Asked inAmazonMicrosoft
#sequenced collections#java 21#list#linkedhashmap#reversed
Report issue

Short Answer

Before Java 21, only some collection types (List, Deque) had a defined encounter order with first/last access, and each exposed it differently — list.get(0)/list.get(list.size()-1), deque.getFirst()/getLast(), while LinkedHashMap and TreeMap had no uniform way to get the first/last entry or an easy reversed view at all. Java 21's SequencedCollection, SequencedSet, and SequencedMap interfaces retrofit a single consistent contract — getFirst(), getLast(), addFirst(), addLast(), and reversed() — across List, LinkedHashSet, LinkedHashMap, TreeMap and more, without changing any existing class hierarchy.

On this page

Concept Made Simple

Coffee Chat Question

“What do SequencedCollection, SequencedSet and SequencedMap (Java 21) fix that List and LinkedHashMap couldn't?”

Remember It Faster

Mind Map Answer

Before JEP 431, 'give me the first/last element' meant a different call per type — and some ordered types (LinkedHashMap, TreeMap) had no direct API for it at all, forcing awkward workarounds like entrySet().iterator().next(). The new interfaces retrofit a single contract onto existing types with no class-hierarchy changes — pure interface addition.

SequencedCollection
getFirst/getLast, addFirst/addLast, reversed() — List, Deque implement it
SequencedSet
LinkedHashSet, TreeSet — ordered iteration + first/last
SequencedMap
LinkedHashMap, TreeMap — firstEntry/lastEntry, reversed()
reversed()
Returns a live reversed VIEW, not a copy

Key takeaway: this is a retrofit, not a new data structure — recognizing it in an interview shows you're current with Java 21+ without needing to know internals, just the consistent first/last/reversed contract.

Learn by Doing

Hands-on Keyboard

java
LinkedHashMap<String, Integer> scores = new LinkedHashMap<>();
scores.put("alice", 90);
scores.put("bob", 75);

// Before Java 21: no direct API for first/last entry
// Java 21+:
var first = scores.firstEntry();   // alice=90
var last = scores.lastEntry();     // bob=75
var view  = scores.reversed();     // live view, iterates bob -> alice

List<Integer> nums = new ArrayList<>(List.of(1, 2, 3));
nums.addFirst(0);                  // SequencedCollection method on List now

Think Beyond the Expected

What If?

Does calling .reversed() copy the collection?

No — it returns a live, reversed VIEW backed by the original collection. Mutations through either the original or the reversed view are visible in both, the same relationship Collections.unmodifiableList() or a Map's keySet() has with its backing collection.

Real World

This mostly shows up as a quality-of-life win in code review — a team migrating to Java 21 replaces custom 'get last inserted entry' helper methods with the built-in lastEntry()/getLast(), and reversed() replaces manual Collections.reverse()-on-a-copy patterns that previously mutated or duplicated the source unnecessarily.

Real Talk from Guru

If this comes up, I'd mention it shows I keep current with LTS releases beyond just 'virtual threads and records' — SequencedCollection is a small, practical Java 21 addition that removes real per-type inconsistency, not a headline feature but a genuine daily-use improvement.

Interviewer's Expectation

Keywords they're listening for:

  • Knows this is a Java 21 (JEP 431) addition
  • Names getFirst/getLast/addFirst/addLast/reversed()
  • Knows reversed() is a live view, not a copy
  • Knows it's a retrofit onto existing types, not a new collection

Common Mistakes

  • Assuming reversed() returns a new independent collection
  • Not knowing LinkedHashMap/TreeMap gained first/last access
  • Confusing this with a brand-new collection implementation

Best Practices

  • Replace custom first/last helper methods with the built-in API on Java 21+
  • Remember reversed() is a live view when reasoning about mutation
  • Use SequencedMap.firstEntry()/lastEntry() instead of iterator-based workarounds

Follow-up Questions

  1. Which existing collection types now implement these new interfaces?
  2. Why is reversed() a view instead of a copy — what's the trade-off?
  3. Does TreeMap's natural ordering interact with SequencedMap in any special way?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Sequenced Collections (Java Collections)
Interview question: "What do SequencedCollection, SequencedSet and SequencedMap (Java 21) fix that List and LinkedHashMap couldn't?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.