Can we convert List to ArrayList?
Converting between List and Array is a very common operation in Java. Show 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 Stringimport java.util.ArrayList; import java.util.List; public class ConvertArrayListToArray { public static void main(String[] args) { ListWe can use the same approach to convert List of Integers to Array of Integers, for example: Related: Convert List to Array of Integersimport java.util.ArrayList; import java.util.List; public class ConvertArrayListToArray { public static void main(String[] args) { ListConvert String Array to ListYou 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"}; ListSometimes 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 ListThere are two built-in ways to convert Array to List in Java.
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"}; ListThe 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 5So 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.
ExampleLive 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 ListOutput[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 Listimport 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. |