Prefer Map.of(..)
MSALPrefer Map.of(..) instead of using java.util.Map#put(..) in Java 10 or higher. Two input shapes are recognised:
- Anonymous-class initialization (
new HashMap<>() {{ put(k, v); ... }}), which is replaced wholesale withMap.of(k, v, ...)(orMap.ofEntries(...)past ten entries) — immutable result. - A
new HashMap<>()declaration followed by a chain oftarget.put(k, v)statements, which is collapsed tonew HashMap<>(Map.of(..))(ornew HashMap<>(Map.ofEntries(..))) — preserving the mutableHashMap.
org. openrewrite. java. migrate. util. UseMapOfmod run . --recipe= org. openrewrite. java. migrate. util. UseMapOfInstall it once from Recipe actions, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.