Java Array to List (ArrayList) Conversion

Filed Under: Java

Sometimes we need to convert Array to List in java, here we will learn two different ways to achieve this. Since List is an interface and ArrayList is the most popular implementation, it’s the same as converting an Array to ArrayList. This situation can come when you are invoking some third party classes returning an array and then you need to change them to list, or to add some more data to the list.

Java Array to List

There are two built-in ways to convert Array to List in Java.

  1. Arrays.asList(T… a): This is the simplest way to convert Array to ArrayList in java but this method returns the underlying representation of the array in the form of ArrayList. The returned ArrayList is fixed-sized and any attempt to modify that will result in UnsupportedOperationException at runtime. Also, any change in the array will change the elements in ArrayList also.
  2. Collections.addAll(ArrayList<T> strList, T[] strArr): This is the best way to convert array to ArrayList because the array data is copied to the list and both are independent object. Once the array is copied, you can modify both the objects independently. Collections is a very useful class in Java Collections Framework that provides a lot of utility methods.

Now let’s see both these methods usage in action.


package com.journaldev.util;


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


public class ArrayToArrayList {

    /**
     * This class shows different methods to convert Array to ArrayList
     * 
     * @param args
     */
    public static void main(String[] args) {
        String[] strArr = {"1", "2", "3", "4"};
        List<String> strList = new ArrayList<String>();
        //return the list representation of array
        //any change in array elements will change the arrayList elements also
        strList = Arrays.asList(strArr);
        System.out.println("Original ArrayList from Arrays.asList()");
        for (String str : strList)
            System.out.print(" " + str);
        //change the array element and see the effect is propogated to list also.
        strArr[0] = "5";
        System.out.println("\nChange in array effect on ArrayList");
        for (String str : strList)
            System.out.print(" " + str);
        //below code will throw java.lang.UnsupportedOperationException because
        // Arrays.asList() returns a fixed-size list backed by the specified array.
        //strList.add("5");

        strList = new ArrayList<String>();

        Collections.addAll(strList, strArr);
        //change both the array and arraylist and check if they are independent?
        strList.add("5");
        strArr[0] = "1";
        System.out.println("\nArray to ArrayList using Collections.addAll()");
        for (String str : strList)
            System.out.print(" " + str);

    }

}

The output of the above program is:


Original ArrayList from Arrays.asList()
 1 2 3 4
Change in array effect on ArrayList
 5 2 3 4
Array to ArrayList using Collections.addAll()
 5 2 3 4 5
Java Array To List Arraylist

Java Array To List

So now you know which method to use to convert Array to ArrayList based on the requirements.

Here is the video tutorial explaining it in Eclipse.

You can find more Java Array examples from our GitHub Repository.

Comments

  1. ES says:

    public static void main(String…args)
    {
    //array to arrayList
    String[]a=new String[4];
    List l=new ArrayList();
    a[0]=”e”;
    a[1]=”k”;
    a[2]=”t”;

    String s=Arrays.toString(a);
    System.out.println(s);
    l = Arrays.asList(a);
    // l.add(“p”); fixed length string
    //List l=new ArrayList(Arrays.asList(a));
    System.out.println(l);
    a[1]=”m”;

    System.out.println(l);
    l.set(0, “u”);
    System.out.println(Arrays.toString(a));

    // second way
    System.out.println(“second scenario”);
    String[]ae=new String[3];

    ae[0]=”e”;
    ae[1]=”k”;
    ae[2]=”t”;

    String se=Arrays.toString(ae);
    System.out.println(se);

    List le=new ArrayList(Arrays.asList(ae));
    le.add(“p”); //fixed length string
    System.out.println(le);
    a[1]=”m”;

    System.out.println(le);
    l.set(0, “u”);
    System.out.println(Arrays.toString(ae));

    }
    }

    Converting arrays to arraylist : in first scenario arraylist and array are interlinked changes to one are getting reflected to other but is second scenario its not…please help me with the difference

  2. infoj says:

    With Java 8 Stream there is one more option –
    If I have an Array of cities say cityArray then it can be converted to ArrayList like this –
    List cityList = Stream.of(cityArray).collect(Collectors.toList());

  3. Sandeep says:

    Awesome explanation buddy.The best thing I like about the whole presentation is that you were explaining the concept slowly but steadily,which will not only help the experts but also the beginners to understand the concept from the root.Thanks a lot Pankaj.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages