Can we convert List to ArrayList?

Converting between List and Array is a very common operation in Java.

The best and easiest way to convert a List into an Array in Java is to use the .toArray[] method.

Likewise, we can convert back a List to Array using the Arrays.asList[] method.

The examples below show how to convert List of String and List of Integers to their Array equivalents.

Convert List to Array of String

import java.util.ArrayList; import java.util.List; public class ConvertArrayListToArray { public static void main[String[] args] { List itemList = new ArrayList[]; itemList.add["item1"]; itemList.add["item2"]; itemList.add["item3"]; String[] itemsArray = new String[itemList.size[]]; itemsArray = itemList.toArray[itemsArray]; for[String s : itemsArray] System.out.println[s]; } }

We can use the same approach to convert List of Integers to Array of Integers, for example:

Related:

Convert List to Array of Integers

import java.util.ArrayList; import java.util.List; public class ConvertArrayListToArray { public static void main[String[] args] { List intList = new ArrayList[]; intList.add[10]; intList.add[20]; intList.add[30]; Integer[] intArray = new Integer[intList.size[]]; intArray = intList.toArray[intArray]; for[Integer i : intArray] System.out.println[i]; } }

Convert String Array to List

You can also convert an Array back to a List. To do this, we use Arrays.asList[]. For example:

import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ConvertArrayToList { public static void main[String[] args] { String[] stringArray = {"item 1", "item 2", "item 3", "item 4"}; List stringList = new ArrayList[Arrays.asList[stringArray]]; for [String listItem : stringList] { System.out.println[listItem]; } } }

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 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 strList = new ArrayList[]; //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[]; 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

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.

Java8Java Programming Object Oriented Programming

We can convert an array to arraylist using following ways.

  • Using Arrays.asList[] method - Pass the required array to this method and get a List object and pass it as a parameter to the constructor of the ArrayList class.

  • Collections.addAll[] method - Create a new list before using this method and then add array elements using this method to existing list.

  • Iteration method - Create a new list. Iterate the array and add each element to the list.

Example

Live Demo

import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Tester {    public static void main[String args[]] {       String[] array = {"a", "b", "c", "d", "e"};       //Method 1       List list = Arrays.asList[array];                 System.out.println[list];       //Method 2       List list1 = new ArrayList[];       Collections.addAll[list1, array];       System.out.println[list1];       //Method 3       List list2 = new ArrayList[];       for[String text:array] {          list2.add[text];       }       System.out.println[list2];    }   }

Output

[a, b, c, d, e] [a, b, c, d, e] [a, b, c, d, e]

Published on 23-Jul-2018 08:31:19

import java.util.ArrayList; class Main { public static void main[String[] args] { ArrayList languages = new ArrayList[]; // Add elements in the list languages.add["Java"]; languages.add["Python"]; languages.add["JavaScript"]; System.out.println["ArrayList: " + languages]; // Create a new array of String type String[] arr = new String[languages.size[]]; // Convert ArrayList into the string array languages.toArray[arr]; System.out.print["Array: "]; for[String item:arr] { System.out.print[item+", "]; } } }

Output

List: [Java, Python, JavaScript] Array: Java, Python, JavaScript,

In the above example, we have created an list named languages. Here, we have used the ArrayList class to implement the List.

Notice the line,

languages.toArray[arr];

Here, the toArray[] method converts the list languages into an array. And stores it in the string array arr.

Note: If we don't pass any argument to the toArray[] method, the method returns an array of the Object type.

Example 2: Convert Java Array to List

import java.util.Arrays; import java.util.ArrayList; import java.util.List; class Main { public static void main[String[] args] { // create an array String[] array = {"Java", "Python", "C"}; System.out.println["Array: " + Arrays.toString[array]]; // convert array to list List languages= new ArrayList[Arrays.asList[array]]; System.out.println["List: " + languages]; } }

Output

Array: [Java, Python, C] List: [Java, Python, C]

In the above example, we have created an array of String type. Notice the expression,

Arrays.asList[array]

Here, the asList[] method of the Arrays class converts the specified array into a list.

Video liên quan

Chủ Đề