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