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:

  1. Recursive modification. In Java 8, writing to the same map from inside HashMap.computeIfAbsent would "just work" (sometimes with a wrong result). On Java 9+ it is a ConcurrentModificationException. In ConcurrentHashMap it is an IllegalStateException. I grep the code for lambdas inside computeIfAbsent/compute/merge and review them.

  2. Order. Tests and reports that rely on HashMap order across minor updates within Java 8 and later versions. And Set.of/Map.of introduced in new code change the order on every start.

  3. New factories and null. During a migration, teams start replacing Arrays.asList/Collections.unmodifiableList with List.of, and collect(toList()) with toList(). List.of doesn't accept null and throws an NPE on contains(null); toList() is immutable. I require these replacements to be done in separate PRs, with tests.

  4. Sequenced Collections (21). Classes that have their own methods named getFirst, removeLast, reversed, especially those that implement both List and Deque. If you have Kotlin or other JVM languages, the Java methods can get picked instead of extension functions.

  5. 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.