Medium👤 3-5 years👤 8-15 years 2 min read

Java 21 Sequenced Collections — Interview Questions

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.

Coffee Chat Question

Concept Made Simple

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

🧠Mind Map Answer

Remember It Faster

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.

SequencedCollectiongetFirst/getLast, addFirst/addLast, reversed() — List, Deque implement it
SequencedSetLinkedHashSet, TreeSet — ordered iteration + first/last
SequencedMapLinkedHashMap, 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.

⌨️Hands-on Keyboard

Learn by Doing

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

🔥What If?

Think Beyond the Expected

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) additionNames getFirst/getLast/addFirst/addLast/reversed()Knows reversed() is a live view, not a copyKnows 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

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

🧩Related Technologies

ListLinkedHashMapTreeMapDeque

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?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions