0

I want to know the difference between entrySet and sequencedEntrySet methods in LinkedHashMap in Java 21+.

I am not sure why we have sequencedEntrySet since LinkedHashMap guarantees the insertion order.

2
  • 2
    Suppose that some time in the future someone edits the code and changes LinkedHashMap to just HashMap then entrySet will still compile but sequencedEntrySet won't so if you use that they will see that you need a defined order. Commented Sep 12, 2025 at 10:46
  • 2
    simple answer: LinkedHashMap implements both Map and SequencedMap - the first requires entrySet(), the second sequencedEntrySet() - so both must be provided -- btw, the maps tag states: "A map is a visual representation of an area ... For the key-value data structure, use dictionary tag instead." (emphasis mine) Commented Sep 12, 2025 at 18:45

1 Answer 1

7

The difference is in the type. entrySet and keySet return Set, whereas sequencedEntrySet and sequencedKeySet return SequencedSet. That interface extends Set but adds method reversed.

If you're thinking "but why didn't Oracle just change the return type of entrySet and keySet from Set to SequencedSet", the answer is binary compatibility. With source compatibility it would be allowed, because it would be an example of covariant return type. Any existing code would compile. But with binary compatibility, the return type is part of the signature. If the return type had been changed, then any pre-compiled library or application that used the method would break with a NoSuchMethodError.

Sign up to request clarification or add additional context in comments.

5 Comments

Binary compatibility refers to the ability of precompiled Java code to run correctly without recompilation, even if the underlying library or API it depends on has changed, as long as the changes preserve the binary-level contract.
Actually, changing the return type of entrySet() in LinkedHashMap would have worked for callers, because the compiler would insert a bridge method to conform to the Map interface, which still declares the method with the Set return type. The problems start with existing subclasses of LinkedHashMap, because those classes would override the Set<Map.Entry<K,V>> entrySet() method but not the hypothetical SequencedSet<Map.Entry<K,V>> entrySet() method.
It would have worked if the caller used Map as declared type, but not if they explicitly used LinkedHashMap. And you're absolutely right about sub classes, I completely forgot about those.
It would also work when explicitly using LinkedHashMap; the bridge method would be inserted in that specific class.
An example of late introduction of a covariant return type, is ByteBuffer.position(int) which was changed in Java 9 to return ByteBuffer instead of Buffer. It works, because the compiler inserts a bridge method to override the inherited method of Buffer and there are no subclasses outside the JDK. The latter precondition is what makes the difference to LinkedHashMap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.