
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Ways to Convert an Array to ArrayList in Java
When working with collections in Java, you might need to convert an array to an ArrayList. This is useful because ArrayList offers more flexibility, such as dynamic sizing and built-in methods for adding, removing, and searching elements.
Need for Converting an Array to an ArrayList
Arrays in Java have a fixed size and provide limited functionality. ArrayList, on the other hand, is part of the Java Collections Framework and provides methods to modify data dynamically. It is useful when -
-
Need to use dynamic Collections.
-
Need to add or remove elements.
-
Need to use the utility methods of List interface
Different Ways to Convert an Array to an ArrayList
There are different methods we can use to convert an Array to an ArrayList, and they are -
-
Using add() method
-
Using Arrays.asList() method
-
Using new ArrayList<>(Arrays.asList(array))
-
Using Collectors.addAll() method
-
Using a for Loop
-
Using Java Streams
Using add() method
To convert an array to an ArrayList, create an empty ArrayList and add() method of the ArrayList class accepts an element and adds it to the current ArrayList.
import java.util.ArrayList; import java.util.Iterator; public class ArrayToArrayList { public static void main(String args[]) { String stringArray[] = {"JavaFX", "Java", "WebGL", "OpenCV", "OpenNLP", "JOGL", "Hadoop", "HBase", "Flume", "Mahout", "Impala"}; ArrayList<String> arrayList = new ArrayList<String>(); for(int i = 0; i < stringArray.length; i++) { arrayList.add(stringArray[i]); } System.out.println("Contents of the array list: "); Iterator it = arrayList.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
Let us compile and run the above program, this will give the following result -
Contents of the array list: JavaFX Java WebGL OpenCV OpenNLP JOGL Hadoop HBase Flume Mahout Impala
Using Arrays.asList() method
The asList() method of the ArrayList class accepts an array and returns a List object. To convert an array to an ArrayList -
import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class ArrayToArrayList { public static void main(String args[]) { String stringArray[] = { "JavaFX", "Java", "WebGL", "OpenCV", "OpenNLP", "JOGL", "Hadoop", "HBase", "Flume", "Mahout", "Impala" }; List <String> list = Arrays.asList(stringArray); ArrayList<String> arrayList = new ArrayList(list); System.out.println("Contents of the array list: "); Iterator it = arrayList.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
Let us compile and run the above program, this will give the following result -
Contents of the array list: JavaFX Java WebGL OpenCV OpenNLP JOGL Hadoop HBase Flume Mahout Impala
Using new ArrayList<>(Arrays.asList(array))
The Arrays.asList() method returns a fixed-size list. This means you cannot add or remove elements from the returned list. But new ArrayList<>(), creates a new, dynamic list that can modify like add, remove, or update elements.
import java.util.ArrayList; import java.util.Arrays; public class ResizableArrayListExample { public static void main(String[] args) { // Step 1: Create a string array String[] colors = { "Red", "Green", "Blue" }; // Step 2: Convert it to a dynamic ArrayList ArrayList < String > colorList = new ArrayList < > (Arrays.asList(colors)); // Step 3: Add and remove elements colorList.add("Yellow"); // Adding new element colorList.remove("Green"); // Removing an element // Step 4: Print the modified ArrayList System.out.println("Updated ArrayList: " + colorList); } }
Let us compile and run the above program, this will give the following result -
Updated ArrayList: [Red, Blue, Yellow]
Using Collectors.addAll() method
The addAll() method of the collection class accepts an ArrayList object and an array as parameters and adds the elements of the given array to the ArrayList.
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; public class ArrayToArrayList { public static void main(String args[]) { String stringArray[] = {"JavaFX", "Java", "WebGL", "OpenCV", "OpenNLP", "JOGL", "Hadoop", "HBase", "Flume", "Mahout", "Impala"}; ArrayList<String> arrayList = new ArrayList(); Collections.addAll(arrayList, stringArray); System.out.println("Contents of the array list: "); Iterator it = arrayList.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
Let us compile and run the above program, this will give the following result -
Contents of the array list: JavaFX Java WebGL OpenCV OpenNLP JOGL Hadoop HBase Flume Mahout Impala
Using a for Loop
By using a for loop, we can add each element of the array to a new ArrayList using a loop. This is used when working with primitive arrays like int[], since methods like Arrays.asList() do not work directly with primitives.
import java.util.ArrayList; public class ArrayToListExample { public static void main(String[] args) { int[] numbers = { 1, 2, 3, 4 }; ArrayList < Integer > list = new ArrayList < > (); for (int num: numbers) { list.add(num); } System.out.println("ArrayList from loop: " + list); } }
Let us compile and run the above program, this will give the following result -
ArrayList from loop: [1, 2, 3, 4]
Using Java Streams
In Java, a Stream is an abstraction that allows you to process sequences of elements (e.g., collections like lists, sets, arrays, or even I/O resources) in a functional way. Java Streams provide a more concise and flexible approach to handling data transformations, computations, and aggregations, compared to the traditional iterative methods.
Java Streams convert an array into a stream using Arrays.stream(). Then, collect(Collectors.toCollection(ArrayList::new)) gathers elements into a new ArrayList.
import java.util.ArrayList; import java.util.Arrays; import java.util.stream.Collectors; public class StreamArrayToList { public static void main(String[] args) { // Step 1: Create a string array String[] languages = { "Java", "Python", "C++", "JavaScript" }; // Step 2: Convert array to ArrayList using streams ArrayList < String > languageList = Arrays.stream(languages) .collect(Collectors.toCollection(ArrayList::new)); // Step 3: Display the result System.out.println("ArrayList: " + languageList); } }
Let us compile and run the above program, this will give the following result -
ArrayList: [Java, Python, C++, JavaScript]