Video in progress
In a big migration from Java 8 to Java 21, which collection-related behavior changes do you check ahead of time?
Code that relies on iteration order, the null and immutability rules of the new Set.of/Map.of/toList(), recursive modifications inside computeIfAbsent now throwing exceptions, and conflicts with the Sequenced Collections method names. I look for them with tests, static analysis and a canary deploy.
📖 Answer
Compile errors are easy to find; the dangerous ones are those whose behavior changed at runtime. My list:
-
Recursive modification. In Java 8, writing to the same map from inside
HashMap.computeIfAbsentwould "just work" (sometimes with a wrong result). On Java 9+ it is aConcurrentModificationException. InConcurrentHashMapit is anIllegalStateException. I grep the code for lambdas insidecomputeIfAbsent/compute/mergeand review them. -
Order. Tests and reports that rely on
HashMaporder across minor updates within Java 8 and later versions. AndSet.of/Map.ofintroduced in new code change the order on every start. -
New factories and null. During a migration, teams start replacing
Arrays.asList/Collections.unmodifiableListwithList.of, andcollect(toList())withtoList().List.ofdoesn't accept null and throws an NPE oncontains(null);toList()is immutable. I require these replacements to be done in separate PRs, with tests. -
Sequenced Collections (21). Classes that have their own methods named
getFirst,removeLast,reversed, especially those that implement bothListandDeque. If you have Kotlin or other JVM languages, the Java methods can get picked instead of extension functions. -
Reflection and serialization. Some libraries (old Kryo, XStream) used reflection to reach internal collection classes. Since Java 17, the module system closes that off (you need
--add-opens).
The process:
- first just build and test all services on the new JDK (without changing code), then make changes;
- static analysis (Error Prone, Sonar) with the new rules;
- a canary: one pod on the new JDK, and its error count and p99 latency are compared with the old pods.
Working with numbers matters: I give teams not a "what will break" list but a list of concrete places found in their own code, so the migration finishes in weeks, not months.
Sign up to mark studied questions and save your progress.