Explain Codes LogoExplain Codes Logo

Initialization of an ArrayList in one line

java
arraylist
stream-api
collections
Nikita BarsukovbyNikita BarsukovΒ·Aug 6, 2024
⚑TLDR

Effortlessly initialize an ArrayList with Arrays.asList for a modifiable collection such as:

ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "C")); // "A", "B", "C"? We're not singing the alphabet song here! πŸ˜„

Post Java 9? Opt for List.of for an immutable list and then wrap it in an ArrayList for mutability:

ArrayList<String> modifiableList = new ArrayList<>(List.of("A", "B", "C")); // "Immutable to mutable". Quite a transformation, isn't it? πŸ›πŸ¦‹

Offering both List.of for efficiency and Arrays.asList for legacy Java versions.

List vs ArrayList: Making the right choice

When it comes to a dilemma between List and ArrayList, make a wise choice of coding to interfaces. This practice paves the path to a more resilient and maintainable code:

List<String> list = new ArrayList<>(Arrays.asList("Element1", "Element2")); // List and ArrayList sitting in a tree, C-O-D-I-N-G! πŸ‘©β€πŸ’»

Tackling the special cases

Single item lists

When your list needs only one element, go for:

List<String> singletonList = Collections.singletonList("SingleValue"); // Lonely wolf of the list world. 🐺

Immutable lists

Does your list not need changes? An immutable list is the way to go. Need to alter things later? Make a mutable copy:

List<String> immutableList = List.of("Immutable", "Elements"); ArrayList<String> copyForModification = new ArrayList<>(immutableList); // Immutability is a virtue, mutability a necessity.

Using Stream API for collections

Put the power of the Stream API in your hands for flexible collection manipulation:

List<String> streamList = Stream.of("Streamed", "Elements").collect(Collectors.toList()); // Streaming service for lists, no subscription required! πŸ“Ί

Efficient addition of elements

Rather than adding elements one by one, save time and add multiple elements:

Collections.addAll(list, "Added", "Elements"); // Welcome aboard, elements! Next station: ArrayList!

Going the extra mile

The power of interfaces

The strength of Java lies in its interfaces. When you don't need to use specific ArrayList features, it's advisable to declare your type as List for flexibility:

List<String> list = Lists.newArrayList("Value1", "Value2"); // Flexibility = πŸ’ͺ

Supplement with third-party libraries

Consider Google Guava's utility method for ArrayList creation and enjoy the clean syntax:

List<String> cities = Lists.newArrayList("New York", "London", "Tokyo"); // No tickets required for this city tour! πŸ›«

Java 16 and above

If you're using Java 16 and above, the toList() method on Stream provides a one-liner syntax for converting stream to list:

List<String> listFromStream = Stream.of("foo", "bar").toList(); // One-liners, saving coders since forever! πŸ¦Έβ€β™€οΈπŸ¦Έβ€β™‚οΈ

Null values and lists

Watch out for nulls! They continue to be the Achilles heel in your lists:

  • List.of and Stream.toList(): exclude null elements.
  • Arrays.asList: null friendly.