Prefer List.of(..)
MSALPrefer List.of(..) in Java 10 or higher. Two input shapes are recognised:
- Anonymous-class initialization (
new ArrayList<>() {{ add("a"); add("b"); }}), which is replaced wholesale withList.of("a", "b")(immutable result, matching the anonymous-class idiom's typical intent). - A
new ArrayList<>()ornew LinkedHashSet<>()declaration followed by a chain oftarget.add(..)statements, which is collapsed tonew ArrayList<>(List.of(..))ornew LinkedHashSet<>(List.of(..))(preserving both the mutable collection and its iteration order).
org. openrewrite. java. migrate. util. UseListOfmod run . --recipe= org. openrewrite. java. migrate. util. UseListOfInstall it once from Recipe actions, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.