Sets
18 questions in 5 subtopics.
HashSet
How isjava.util.HashSet implemented, and how does add() report whether the element was new?A developer writesclass Person {
String id;
public boolean equals(Person other){
return id.equals(other.id);
}
public int hashCode(){
return id.hashCode();
}
}
HashSet<Person> holds duplicates with the same id. Why?A service keeps a HashSet<Long> of about 10 million user ids in memory and the heap is under pressure. Estimate the cost and propose alternatives.Your organisation has many memory-heavy Java services full of boxed collections such as Set<Long> and Map<Integer, Double>. Some teams want to adopt primitive collection libraries, others want to wait for Project Valhalla. How would you decide and what would you standardise?LinkedHashSet
When would you useLinkedHashSet, what is its cost, and what did Java 21's SequencedSet add?You must deduplicate a list of 5 million customer records by email while keeping the first occurrence and original order. Compare stream().distinct(), collecting into a LinkedHashSet, and a manual loop, including the parallel case.Java 21 (JEP 431) retrofitted SequencedCollection, SequencedSet and SequencedMap onto existing types such as LinkedHashSet, List and Deque. Why did such a basic abstraction take until Java 21, and what does retrofitting interfaces onto widely implemented types teach about evolving your own long-lived APIs?TreeSet and NavigableSet
Name theNavigableSet operations of TreeSet that you cannot get from a HashSet, and give a practical use of one.What does this print, and why?Set<String> s = new TreeSet<>(Comparator.comparing(String::length));
s.add("abc");
s.add("xyz");
s.add("hello");
System.out.println(s);
TreeSet<Player> ordered by score descending, then id. Scores update often. Users report players appearing twice or not being found. What is wrong and how would you design it correctly?EnumSet
How isEnumSet implemented and why is it faster than a HashSet of enum constants?A legacy service stores user permissions as an int bit mask in the database and as Set<String> in code. You propose an enum Permission with EnumSet. How would you design storage and evolution so that adding or reordering enum constants is safe?A shared library defines an enum Feature and services exchange EnumSet<Feature> values over JSON and Kafka. Services deploy at different times, so a consumer may receive a feature name it does not know. Design the representation and evolution rules.Set operations
Given twoSet<String> a and b, write code to compute their union, intersection and difference without modifying a or b.A job calls hugeHashSet.removeAll(idList), where hugeHashSet has 200,000 elements and idList is an ArrayList of 1 million ids. It takes minutes. Explain why and fix it.After upgrading code to use Set.of and Map.of for constants, some services produce JSON whose array order changes between restarts, breaking response caching and a few consumer tests. How would you handle this across teams?An audience-targeting service intersects sets of millions of user ids per request (for example users in region X who also bought Y), using HashSet<Long> and retainAll. Latency and memory are growing as data grows. What would you change architecturally?Your codebase uses JDK collections, Guava immutable collections and Eclipse Collections in different places. The JDK has since added List.of and Set.of (9), copyOf (10) and sequenced collections (21). What long-term policy on collection libraries would you set?No question matches these choices.