5 ways to convert Array to List in java


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

Array is static and fixed in size of datastructure. ArrayList is dynamic data structure.

create arraylist from array in java with example

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 an string array and initlized with list of strings.

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

  • List
  • ArrayList
  • LinkedList

using plain for loop

This method is straight forward using for loop.

  • Iterate each string from an array usng for loop
  • add it to arraylist using add method

Here is an example

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

Collections.addAll method

Collections is java class which every list implementation can extend it.

addAll method accepts list and array and returns boolean.

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

This 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 convert fixed size array into collection List implementations.

pass the array into asList method and returns an 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 array as a parameter and
  • Iterate and map each element using stream
  • Collect the elements using collect method
  • returns List using Collectors.toList() method
List<String> list =Arrays.stream(words).collect(Collectors.toList());
System.out.println(list);

This will works with object types, however it will not works with primitive type.

How to convert primitive array ito arraylist of object?

For example,

  • Let’s have a int array and initialized with numbers
  • iterate and map each element using stream method.
  • Boxing each primitive into object using 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 array into List,ArrayList and LinkedList

The following example are 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 mutable list, you can use 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 new lot of new features than java collection.

For maven projects, You have to add 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);
    }
}
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.