Explain Codes LogoExplain Codes Logo

Create ArrayList from array

java
arrays
guava
streams
Alex KataevbyAlex Kataev·Aug 13, 2024
TLDR

To transform an array into a ArrayList swiftly, employ the Arrays.asList() function wrapped with a constructor:

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

Remember: Arrays.asList() in its pure form implies an immutable list. Enveloping it with new ArrayList<>() guarantees that it becomes modifiable.

Deeper insights

The philosophy of Arrays.asList()

The utility Arrays.asList() is a tremendous shortcut for initializing a list from an array. However, it comes with a peculiarity: the generated list is immutable and linked to the original array like Siamese twins. When you alter one, the other mirrors it.

Primitives: The troublemakers

Primitives like int[] unceremoniously disrupt the flow when used with Arrays.asList(). Autoboxing, or automatic conversion from primitive to wrapper, is not supported. The workaround for these rebels is making use of Java 8 streams for boxing purpose.

int[] intArray = {1, 2, 3}; // Turning our little troublemaker into a well-behaved List List<Integer> integerList = Arrays.stream(intArray).boxed().collect(Collectors.toList());

The Guava to the rescue

Here comes the Guava library, our supercharged toolbox. To effortlessly create a modifiable ArrayList, use Lists.newArrayList(array). Need an immutable list? Don't fret, ImmutableList.copyOf(array) has got you covered.

Leveraging Java 7's diamond

Keeping up with the evolution is crucial in Java. The diamond operator <> removes redundant type specification on RHS, making the code cleaner and faster to write.

List<String> list = new ArrayList<>();

Advanced techniques and alternatives

Freeze! Collections.unmodifiableList

If utter immutability is what you seek, Collections.unmodifiableList() is your bulwark against any unwelcomed alterations.

List<String> immutableList = Collections.unmodifiableList(Arrays.asList(array));

Varargs and Arrays.asList(): The dynamic duo

An underappreciated feature of Arrays.asList() is accepting varargs for a sleek inline list creation.

List<String> fruits = Arrays.asList("apple", "banana", "cherry"); // This is no magic trick! It's simple Java.

Custom type arrays: Isn't it straightforward?

For custom type arrays, the conversion process is plain sailing. However, beware of ClassCastException! Ensure type consistency to keep this trouble at bay.

Element[] elements = {new Element(1), new Element(2)}; List<Element> elementList = new ArrayList<>(Arrays.asList(elements));

DIY: Create ArrayList manually

One must never forget the roots! If it wasn't for the manual iteration, none of this would have been possible. This good old method comes in handy whenever complex conversion logic arises.

String[] array = {"apple", "banana", "cherry"}; List<String> list = new ArrayList<>(); // The good old fashioned way for (String item : array) { list.add(item); // Adding one by one, just like in preschool. }