Quick revision
The short answer
I would make JDK collection interfaces the only collection types in public APIs between modules and services, use the JDK's own immutable factories by default, and allow Guava or Eclipse Collections only inside module boundaries where they add something the JDK lacks, such as primitive collections or multimaps. Dependencies the JDK now covers should be removed over time, opportunistically and with care for behaviour differences such as iteration order, under a written policy that is checked automatically.
What to remember
In this answer, we set a long-term policy: JDK collection interfaces in every public API between modules and services, JDK immutable factories by default, and Guava or Eclipse Collections only inside modules where they add primitive collections or structures the JDK lacks. We saw that migrations need care because Guava's ImmutableSet keeps insertion order and drops duplicates while Set.of randomizes order and throws on duplicates, and we planned to write the policy down, enforce boundaries with ArchUnit, migrate opportunistically and remove dependencies as their last use disappears.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would make JDK collection interfaces the only collection types in public APIs between modules and services, use the JDK's own immutable factories by default, and allow Guava or Eclipse Collections only inside module boundaries where they add something the JDK lacks, such as primitive collections or multimaps. Dependencies the JDK now covers should be removed over time, opportunistically and with care for behaviour differences such as iteration order, under a written policy that is checked automatically.
Why this question is asked
Large codebases accumulate libraries in layers. Guava's immutable collections arrived when the JDK had nothing comparable, Eclipse Collections came in for a performance-sensitive module, and meanwhile the JDK added List.of and Set.of in Java 9, copyOf in Java 10, Collectors.toUnmodifiableList, Stream.toList in Java 16 and sequenced collections in Java 21. Without a policy, every team chooses differently, and the result is mixed types at module boundaries, conversion code everywhere and several libraries to upgrade forever. Interviewers ask senior engineers this to see how they balance consistency, migration risk and real technical needs. In this answer, we will look at the costs of the current mix, set the rule for public APIs, examine the behaviour differences that make migration risky, define when third-party collections stay, and plan how to write down and roll out the policy.
1What the Mix Costs Today
Three collection libraries are not a problem in themselves. The problems come from where they appear. When a module's public method returns an ImmutableList from Guava, every caller must depend on Guava, and a change of Guava version in one module can force upgrades in many others. When one service exposes Eclipse Collections types in a client library, consumers inherit that dependency too.
There is also a cognitive cost. Developers must remember which immutable type rejects nulls, which keeps order, and how each library's copy methods behave. And every dependency carries long-term cost: upgrades, compatibility testing, licence review and security advisories, all of which recur for as long as the codebase lives, which may be ten years or more.
2The Rule for Public APIs
The first and most important rule is that anything crossing a module or service boundary uses JDK interfaces: List, Set, Map, Collection, and since Java 21 SequencedCollection, SequencedSet and SequencedMap where order matters. Implementations behind those interfaces can be anything, but callers never see a third-party type, so they are never forced to depend on one.
Notice that the JDK interfaces do not express immutability in the type, which is the one real loss compared with Guava's ImmutableList. We compensate with documentation and by always returning unmodifiable collections, so the convention is: what we return cannot be changed, and what we receive we do not change.
3Behaviour Differences That Make Migration Risky
Replacing Guava's immutables with JDK factories looks like a mechanical find-and-replace, but the two behave differently in ways that break programs. The biggest one is order. Guava's ImmutableSet and ImmutableMap keep insertion order, and plenty of code relies on that without saying so. Set.of and Map.of have unspecified iteration order that changes between JVM runs, so a migration can silently reorder JSON output, generated reports or the order of checks.
| Guava or Eclipse type | Depends on order? | JDK replacement |
|---|---|---|
ImmutableList | yes, by definition | List.of, List.copyOf, Stream.toList |
ImmutableSet | yes, often implicitly | unmodifiable LinkedHashSet, or List |
ImmutableSet | no, lookups only | Set.of, Set.copyOf |
ImmutableMap | yes | unmodifiable LinkedHashMap |
Multimap, Table, RangeSet | n/a | no JDK equivalent: keep inside the module |
IntList, LongSet (primitive) | n/a | no JDK equivalent: keep inside the module |
Duplicates differ too: Set.of throws IllegalArgumentException on a duplicate argument, while ImmutableSet.of quietly drops it. Both reject nulls, but code that converted from mutable collections may meet nulls it never saw before. So the safe replacement for an order-dependent ImmutableSet is often an unmodifiable LinkedHashSet or a List, not Set.of.
4When Third-Party Collections Remain Justified
The JDK still has no primitive collections. A Map<Long, Integer> boxes every key and value, which for a hot path with tens of millions of entries means gigabytes of heap and heavy GC. Eclipse Collections or fastutil provide LongIntHashMap-style maps that store primitives in arrays, often five to ten times smaller. Guava's Multimap, Table, BiMap and RangeSet also have no JDK equivalents, and rewriting them by hand produces more code and more bugs than the dependency costs.
So the policy allows these, with two conditions: they stay inside module boundaries, never in public signatures, and the module's owners can explain the need in a sentence, such as primitive collections in the pricing engine to keep heap under 4 GB. That makes each use a deliberate decision rather than a habit.
5Writing the Policy Down and Enforcing It
A policy only works if people can find it and tools check it. I would write one page with the rules: JDK interfaces at boundaries, JDK factories by default, third-party collections only with a stated reason inside a module, and a list of known behaviour differences for migrations. Then we enforce the boundary rule automatically, for example with an ArchUnit test that fails the build if a public method of an API package returns or accepts a type from com.google.common.collect or org.eclipse.collections.
The migration itself is opportunistic. When a team changes a class for other reasons, it replaces Guava immutables there, with tests around ordering. Large automated rewrites are tempting but risky because of the order and duplicate differences, and they create huge diffs that nobody reviews properly. Tools like OpenRewrite can help with the mechanical part once the risky cases are flagged. Each quarter we look at the remaining usage, and when a library's last use in a module disappears, we remove the dependency from that module's build.
- Does the type cross a module or service boundary?YesUse a JDK interfaceNoInside the module: continue
- Does the JDK cover the need?YesUse the JDK type or factoryNoCheck the allowed exceptions
- Primitive collection or Multimap-like structure?YesAllowed, note the reasonNoUse the JDK and write the small helper
- ArchUnit checks public signatures in CI
4 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why not ban Guava entirely?
Because Guava provides more than collections, and some of its collection types have no JDK equivalent. A total ban would push teams to write their own multimaps or caches, which costs more than the dependency. The goal is to stop leaking it across boundaries and to stop using it where the JDK now does the same job.
How do you handle libraries that return Guava types in their own APIs?
We wrap them at the edge of our module and convert to JDK types, so the third-party type stays an implementation detail. If the library is internal, we ask its owners to follow the same boundary rule in the next major version.
Do sequenced collections change the policy?
They improve it. Before Java 21 there was no JDK interface that said this collection has a defined order, so APIs used List even for sets, or documented order in comments. Now SequencedSet and SequencedMap let us express ordered, duplicate-free results in a public signature without any third-party type.
7Conclusion
In this answer, we set a long-term policy: JDK collection interfaces in every public API between modules and services, JDK immutable factories by default, and Guava or Eclipse Collections only inside modules where they add primitive collections or structures the JDK lacks. We saw that migrations need care because Guava's ImmutableSet keeps insertion order and drops duplicates while Set.of randomizes order and throws on duplicates, and we planned to write the policy down, enforce boundaries with ArchUnit, migrate opportunistically and remove dependencies as their last use disappears.
Open with
My policy would start at the boundaries. Any public API between modules or services uses JDK interfaces, List, Set, Map and since Java 21 the sequenced interfaces where order matters, so no caller is ever forced to depend on Guava or Eclipse Collections.
Then, if they want more
By default new code uses the JDK factories, List.of, copyOf, Stream.toList, because the JDK now covers what Guava's immutables were originally brought in for. I would be careful about migration, though, because the behaviour is not identical.
Guava's ImmutableSet keeps insertion order, while Set.of has unspecified order that changes between JVM runs, and Set.of throws on duplicates where ImmutableSet drops them. So an order-dependent ImmutableSet should become an unmodifiable LinkedHashSet or a List, not Set.of, and migrations need tests around ordering.
Third-party collections remain justified inside a module when the JDK has nothing equivalent, mainly primitive collections for hot paths with millions of entries, and structures like Multimap or RangeSet, as long as the team can state the reason. Every dependency costs upgrades and security reviews for years, so when the JDK covers a use we remove it.
I would write the policy on one page, enforce the boundary rule with an ArchUnit test in CI, and migrate opportunistically when teams touch code anyway, rather than one big rewrite that nobody can review, then drop each library from a module when its last use goes.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 10, Java 16, Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your codebase uses JDK collections, Guava immutable collections and Eclipse Collections...": https://scrivio-interview-library.netlify.app/java/sets/de7ad918c1/