How to convert Array to List in Java

This is a very basic operation in our day to day programming in which we transform an Array to a List and vice versa.

Here in this tutorial, we will discuss various ways of transforming an array into a list.
The three approaches that I will discuss today are –

  • Using Arrays.asList(T… a)
  • Using Collections.addAll( Collection<? super T> c, T… a )
  • Using Java 8 streams API

Using Arrays.asList(T… a)

As per the official documentation of this method –

Returns a fixed-size list backed by the specified array. (Changes to the returned list “write through” to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection#toArray. The returned list is serializable and implements RandomAccess.

So in simpler words, this returns a List which is actually backed by the array and hence it is suitable when we want to create a List of fixed size which is already initialized with the values of the array passed to it.

Its time complexity is O(1).

Example of Arrays.asList

package codingeek;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayToListConversion {

  //Arrays.asList()
  public static void main(String[] args) {
     final String arr[] = new String[] { "C", "O", "D", "I","N", "G", "E", "E", "K" };

     // convert array to list - FIXED SIZE IMMUTABLE LIST
     final List<String> list = Arrays.asList(arr);
     System.out.println("Immutable list - " + list);

     // convert array to list - Non immutable list, can add and perform other operations
     final List<String> list2 = new ArrayList<>(Arrays.asList(arr));
     System.out.println("Non Immutable list - " + list2);

  }

}

Output:-
Immutable list - [C, O, D, I, N, G, E, E, K]
Non Immutable list - [C, O, D, I, N, G, E, E, K]

Using Collections.addAll( )

As per the official documentation of this method –

Adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations.

So in simpler words, it will return a final list that will have all the elements that already exists in the list passed(first argument) along with the additionally mentioned elements.

Its time complexity is O(n).

Example of Collections.addAll( )

package codingeek;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayToListConversion {

      //Collections.addAll()
   public static void main(String[] args) {
       final String arr[] = new String[] { "G", "E", "E", "K" };
       final List<String> initialList = new ArrayList<String>() {{
           add("C");
           add("O");
           add("D");
           add("I");
           add("N");
       }};

       // Elements of the array are appended at the end
       Collections.addAll(initialList, arr);
       System.out.println(initialList);
   }

}

Output:-
[C, O, D, I, N, G, E, E, K]

Using Java 8 Streams

Java 8 streams also provide a simple way to transform an array to the List.
In this, we create a stream of array and get the list using the collector Collectors.toList() but there is a twist if we are converting an array of primitives i.e. int, float, long etc then we have to use an extra intermediate function ( boxed() ) to convert the primitives into their Wrapper classes.

Have a look at examples for clear understanding.

Java 8 stream – Convert array to List

package codingeek;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayToListConversion {

    //Java 8 Streams
    public static void main(String[] args) {
        final String arr[] = new String[] { "C", "O", "D", "I","N", "G", "E", "E", "K" };
        final List<String> convertedList = Arrays.stream(arr).collect(Collectors.toList());

        System.out.println(convertedList instanceof List);

        //Adding new item successfully - i.e. non immutable list
        convertedList.add("new Item");

        System.out.println(convertedList);

        //Example with int array(primitive).
        final int[] arr2 = {1, 2, 3};
        System.out.println(Arrays.stream(arr2).boxed().collect(Collectors.toList()));
    }
}

Output:-
true
[C, O, D, I, N, G, E, E, K, new Item]
[1, 2, 3]

Do come back for more. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback.

Share and subscribe.

Keep Coding!! Happy Coding!! 🙂

References – 

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index