How can I initialize an `ArrayList` in one line in Java?

If your list needs to undergo some dynamic processing or transformation, using Streams can be a powerful approach for initializing your java arraylist initialize.

ArrayList<String> places = new ArrayList<>(
    Stream.of("Buenos Aires", "Córdoba", "La Plata")
          .collect(Collectors.toList())
);

:white_check_mark: Why?

  • Ideal if you need to perform filtering, mapping, or other transformations right when initializing the list.
  • Powerful and flexible for more complex use cases. :x: But: Overkill for static, simple lists. If you’re just initializing with hardcoded values, this might be more complexity than needed.