Explain Codes LogoExplain Codes Logo

How to declare an ArrayList with values?

java
list-declaration
arraylist
java-8
Anton ShumikhinbyAnton Shumikhin·Oct 5, 2024
TLDR

Want quick results? Pre-fill an ArrayList using:

Initialization Block:

ArrayList<String> list = new ArrayList<String>() {{ // Fill 'er up! add("A"); add("B"); add("C"); // There's a reason garbage trucks are so big! }};

Arrays.asList:

ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "C")); // Who knew arrays were so sociable?

Either way leads to an ArrayList with elements "A", "B", and "C".

Untangling the methods of ArrayList declaration

Let's divide and conquer with some understandable code snippets to illustrate different declaration methods.

Java 9+ and Java 8: The cool guys on the block

Java 9+ has an edge with List.of() - it creates an immutable list. No changes allowed!

List<String> list = List.of("A", "B", "C"); // More like List.of("Fun", "Times", "Ahead"), am I right?

Java 8 rocks the Stream for a mutable list.

List<String> list = Stream.of("A", "B", "C").collect(Collectors.toList()); // Streaming: not just for binge-watching anymore.

Ensuring list mutability for changes on the go

If you need to make changes on the go, say hello to ArrayList:

List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C")); // So flexible, it puts yoga masters to shame.

Minimizing clutter with static imports and Guava

Use static import to cut corners:

// Import statement import static java.util.Arrays.asList; // Then use asList directly in your code ArrayList<String> list = new ArrayList<>(asList("A", "B", "C")); // Because we're all about efficiency here, right?

Guess what? Guava just made the list (pun definitely intended):

List<String> list = Lists.newArrayList("A", "B", "C"); // Lists.newArrayList: Because typing four more letters is too mainstream.

Remember to import:

import com.google.common.collect.Lists; // Lists, lists everywhere!

Using generics for type safety

Type safety is a big deal:

ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3)); // The only place where mixing numbers and letters doesn't result in a fatal error.

Mutable vs Immutable

List.of() means no changes allowed.

List<String> list = List.of("A", "B", "C"); // This list is so stubborn it makes a mule look cooperative.

For modifiable gang, use ArrayList.

Initial capacities for performance tuning

If you know list size beforehand set an initial capacity.

ArrayList<String> listWithCapacity = new ArrayList<>(50); // This list has big plans for its future.

Deeper into list declaration approaches

Unravel more conceptual knots on array initialization for common and edge scenarios.

Converting arrays to lists

No Hogwarts degree necessary for this magic trick:

String[] array = {"A", "B", "C"}; List<String> listFromArray = Arrays.asList(array); // "You're a wizard, Harry."

Declaring with List Interface

If you don't need ArrayList methods, use the List interface:

List<String> list = new ArrayList<>(); // Why mention the obvious?

Immutable lists aren't always your friend

Beware! Immutable lists throw tantrums (read: UnsupportedOperationException) if you try to change them. Stick to mutable lists for changing collections.