J.IV · Java 25 practice question
A developer wants to assign a standard Set implementation to a SequencedCollection<String> variable. Which class compiles without a cast?
import java.util.*;
void main() {
SequencedCollection<String> sc = new LinkedHashSet<>();
sc.add("first");
IO.println(sc.size());
}
Check answer and explanation
Correct answer: LinkedHashSet
Explanation
LinkedHashSet is a known implementing class of SequencedCollection (via SequencedSet), so it can be assigned directly to SequencedCollection<String>.
Common trap
HashSet has no defined encounter order and does not implement SequencedCollection. EnumSet is not listed as an implementing class of SequencedCollection and also cannot be instantiated with new.
Source: Java SE 25 API: SequencedCollection
Checked against the cited text on 2026-11-25.