5 ways to convert Array to List in java

In this post, We are going to learn multiple ways to convert the array to ArrayList in java.

The array is static and fixed in the size of the data structure. ArrayList is a dynamic data structure.

Convert Array to ArrayList

For example, Let’s create an array of strings as seen below.

String[] words = new String[] { "one","two","three" };
(or)
String words[] = { "one","two","three" };

Create a string array and initialized with a list of strings.

Now, we are going to see multiple ways to convert this into list implementations.

  • List
  • ArrayList
  • LinkedList

using plain for loop

This method is straightforward using for loop.

  • Iterate each string from an array using for loop
  • add it to ArrayList using the add method

Here is an example

 List<String> list = new ArrayList<>();
        for (String word : words) {
            list.add(word);
        }

Collections.addAll method

Collections are java classes that every list implementation can extend it.

addAll method accepts list and array and returns boolean.

  • We already have the string array declared above
  • first, create a destination empty list
  • pass list and array to addAll() method.
  • arrays are converted into a list
  • list parameter holds the values of an array
List<String> list = new ArrayList<>();
Collections.addAll(list,words);

It runs faster compared to other asList and for loop iteration

java 7 java.util.Arrays method asList

java.util.Arrays has multiple methods to manipulate array in java.

asList method converts the fixed-size array into collection List implementations.

pass the array into the asList method and returns a java.util.List

 List<String> list = Arrays.asList(words);

using java8 streams

Streams introduced in java 8 to manipulate iteration easily. you can learn about stream map in java8

  • Arrays.stream takes an array as a parameter and
  • Iterate and map each element using stream
  • Collect the elements using the collect method
  • returns List using Collectors.toList() method
List<String> list =Arrays.stream(words).collect(Collectors.toList());
System.out.println(list);

It works with object types, however, it does not work with primitive types.

How to convert the primitive array into ArrayList of the object?

For example,

  • Let’s have an int array and initialized with numbers
  • iterate and map each element using the stream method.
  • Boxing each primitive into an object using the boxed() method.
  • Finally, collects and return List method
int[] ints = new int[] {15,21,14,17,25};
List<Integer> listNumbers =Arrays.stream(ints).boxed().collect(Collectors.toList());

Let’s convert the array into List, ArrayList and LinkedList

The following example is sequence of steps

  • iterates the array using stream
  • Convert primitive into object type using boxed() method
  • Collect and return List using Collectors.toList() method
  • Collect and return ArrayList using Collectors.toCollection(ArrayList::new) method
  • Collect and return LinkedList using Collectors.toCollection(LinkedList::new) method
List<Integer> listNumbers =Arrays.stream(ints).boxed().collect(Collectors.toList());

ArrayList<Integer> arrayLists =Arrays.stream(ints).boxed().collect(Collectors.toCollection(ArrayList::new));

LinkedList<Integer> linkedLists =Arrays.stream(ints).boxed().collect(Collectors.toCollection(LinkedList::new));

java9 using List.of method

List added overloaded of() method in java9. You can check java9 features

List.of() is a static factory method returns immuntable list which has array elements.

List<String> immutablewordList = List.of(words);

if you want to return a mutable list, you can use the below approach

ArrayList<String> mutablewordList = new ArrayList<>(List.of(words));

using guava library Lists.newArrayList method

guava is a google core library for java which has a new lot of new features than the java collection.

For maven projects, You have to add the following dependency

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>30.1.1-jre</version>
</dependency>

com.google.common.collect.Lists has a method newArrayList, It takes array and return arraylist.

Here is an example

import java.util.*;
import com.google.common.collect.Lists;

public class Main {
    public static void main(String[] args) {
        String[] words = new String[]{"one", "two", "three"};
        List<String> list = Lists.newArrayList(words);
    }
}