Explain Codes LogoExplain Codes Logo

How can I convert String

java
list-operations
array-conversion
java-8-streams
Alex KataevbyAlex Kataev·Oct 4, 2024
TLDR

For a quick and efficient conversion of String[] to ArrayList<String>, make use of the Arrays.asList() method within a new ArrayList constructor as shown:

String[] fruitArray = {"apple", "banana", "cherry"}; ArrayList<String> fruitList = new ArrayList<>(Arrays.asList(fruitArray));

Key Insight: Arrays.asList() returns a fixed-size list backed by the underlying array. To get a fully modifiable ArrayList, it's crucial to wrap the result into a new ArrayList object.

Breaking down the conversion (with a little humor)

No magic involved, just plain old Java doing its thing. Let's understand this step by step:

Using Arrays.asList

The static utility class Arrays provides the function asList() to create a list backed by the initial array:

String[] fruitArray = {"apple", "banana", "cherry"}; List<String> fruitList = Arrays.asList(fruitArray); // fruitList.add("mango"); // Error here! Like trying to make a fruit salad without a bowl.

Step it up to ArrayList

The returned list is a fixed-length list. So, to make it flexible and modifiable - just like that stretchy pair of sweatpants we all own - simply instantiate a new ArrayList:

ArrayList<String> flexibleFruitList = new ArrayList<>(Arrays.asList(fruitArray));

Add flair with Collections.addAll()

Instead of fumbling through each element like finding the car keys in your pocket, use Collections.addAll():

ArrayList<String> fruitList = new ArrayList<>(); Collections.addAll(fruitList, fruitArray); // Voila, a fruit salad with all your favorite fruits!

Stay old school with the for loop

Sometimes, we like it old-school, right? Maybe you want to add a twist of lemon to each fruit:

ArrayList<String> fruitList = new ArrayList<>(fruitArray.length); // Size with fruitArray.length to avoid rehashing for(String fruit : fruitArray){ fruitList.add(fruit + " with lemon"); }

When you're feeling fancy with Streams

Java 8's Stream API can streamline things with a pinch of functional style:

ArrayList<String> fruitList = Stream.of(fruitArray) .collect(Collectors.toCollection(ArrayList::new)); // Feels like having a robot make your salad, doesn't it?

Dealing with the peculiarities

Fixed-size list pitfall

When using Arrays.asList(), keep in mind that the resulting List is fixed in size and any attempt to modify it will throw an UnsupportedOperationException.

Null elements in the list

If the original array contains null entries, they'll also be part of the ArrayList. To avoid this, take advantage of the Streams API's filtering capability:

ArrayList<String> fruitList = Stream.of(fruitArray) .filter(Objects::nonNull) .collect(Collectors.toCollection(ArrayList::new)); // This filters out the rotten ones!

Synchronized ArrayList

The ArrayList instances created using the methods mentioned above aren't thread-safe. If multiple threads are going to access your ArrayList, consider using Vector or Collections.synchronizedList().