Collections framework overview
23 questions in 8 subtopics.
Collection and Map hierarchy
Describe the main interfaces of the Collections Framework and where Map fits. Why does Map not extend Collection?What are the skeletal implementation classes such asAbstractList and AbstractMap for, and what are the optional operations of the Collection interface?The Collections Framework of 1998 chose optional operations that throw UnsupportedOperationException instead of separate read-only interfaces. Looking at how Java evolved since, what were the consequences, and what would you standardise in your own APIs?Sequenced collections
Using Java 21 sequenced collections, get the first and last elements of a List, aLinkedHashSet and a LinkedHashMap, and iterate a list in reverse. What did this need before Java 21?After upgrading to Java 21, a project fails to compile with a conflict in a class that implements both List and Deque, or behaves differently because a custom List subclass already had a getFirst method returning a different type. Explain why.Your platform is moving from Java 17 to 21. Sequenced collections added new default methods to List, Deque, LinkedHashSet and LinkedHashMap. How do you assess the impact on internal collection implementations and libraries across teams?Choosing a collection
Pick a collection for each case and justify it: unique visitor IDs, a leaderboard ordered by score, a recent-items list with fast removal of the oldest, a lookup of product by SKU that keeps insertion order.A service holds 50 million small entries in aHashMap<Long, Integer> and uses 4 GB of heap. How would you reduce memory, and what would you consider before switching implementation?Teams in your company often add third-party collection libraries such as Guava, fastutil, Eclipse Collections and Vavr. What policy do you set on collection libraries?Collection choice seems local, but over years it affects memory, cost and performance of a whole fleet. How would you make collection and data structure decisions visible and governed at company scale without micromanaging teams?Collections utility methods
What do thesejava.util.Collections methods do and what should you know about each: sort, binarySearch, unmodifiableList, synchronizedList, emptyList, nCopies, frequency, shuffle?Two threads use a Collections.synchronizedList. One iterates with for-each while the other adds, and ConcurrentModificationException appears. Why, and what are the options?Immutable factory methods List.of and Map.of
What are the properties ofList.of, Set.of and Map.of? Why do Set.of("a", "a") and Map.of("k", 1, "k", 2) throw, and why does List.of(1, null) throw?After replacing Arrays.asList with List.of in a codebase, some calls start throwing NullPointerException from list.contains(null) and a test breaks because Set.of iteration order differs between runs. Explain both.You are setting coding standards for returning collections from shared library methods. Should methods return List.of copies, unmodifiable views, or mutable lists? Justify for performance and safety.The JDK added List.of and Map.of in Java 9 with strict rules: no nulls, no duplicates, unspecified iteration order. What do these choices tell you about API design for the long term, and would you apply similar rules to your company's libraries?Converting between collections
What does each line do, and which ones fail or surprise?int[] nums = {3, 1, 2};
List<int[]> a = Arrays.asList(nums);
List<Integer> b = Arrays.asList(3, 1, 2);
b.add(4);
String[] arr = list.toArray(new String[0]);
List<Integer> c = Arrays.stream(nums).boxed().toList();
c.sort(null);
Set<String> s = new HashSet<>(list);
List<String> back = new ArrayList<>(s);
products.stream().collect(Collectors.toMap(Product::sku, Product::discount)). It fails in production with NullPointerException although no product or sku is null. Later a colleague replaces it with a loop into a HashMap and the response order of a JSON field changes. Explain both problems.Profiling a high-traffic service shows that 20 percent of allocation comes from converting between collections: lists copied to sets for contains checks, streams collected to lists and immediately copied again, and defensive copies at every layer. How would you guide the team to reduce this without making the code unsafe?Legacy collections Vector and Hashtable
Vector, Hashtable, Stack and Enumeration are legacy collections. Explain how they differ from their modern replacements and what you would use instead in new code.A large codebase still has hundreds of uses of Vector, Hashtable and Enumeration, some in public APIs used by other teams, and some required by old APIs such asjava.util.Properties and JNDI's InitialContext environment. How do you plan their removal, and where do you stop?Pairs and tuples
Java has no general Pair or Tuple class in the JDK. A method must return both the minimum and maximum of a list. Show three ways to return two values and say which one you would choose in Java 25.In a code review you find methods returningPair<Pair<String, Integer>, List<Pair<Long, BigDecimal>>> and maps keyed by Pair<String, String>. What do you recommend, and what do records change for map keys and grouping?No question matches these choices.