Java Program to Convert an Array into a List
In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or removing elements dynamically. In this article, we’ll explore how to convert an array into a list in Java.
Example 1: Using Arrays.asList() Method
One of the most easy and effective ways to convert an array into a list in Java is by using the Arrays.asList()
method. This method provides a fixed-size list backed by the specified array.
import java.util.Arrays;
import java.util.List;
public class ArrayToList {
public static void main(String[] args) {
// Array declaration
String[] arr = {"geeks", "for", "geeks", "for", "dev"};
// Converting using array to list using Arrays.asList()
List<String> lis = Arrays.asList(arr);
System.out.println("List: " + lis);
}
}
Output
List: [geeks, for, geeks, for, dev]
Explanation: The Arrays.asList()
method converts the given array into a list. The returned list is backed by the original array, which mean that changes to the list will affect the array.
Example 2: Using ArrayList
Constructor
If you want to add or remove the elements, you can use the ArrayList
constructor to convert an array into a list. The ArrayList
constructor can accept a collection, and we can pass a list created from the array.
import java.util.*;
public class ArrayToList{
public static void main(String[] args) {
// Taking an array
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
// Converting array to ArrayList
List<Integer> lis = new ArrayList<>(Arrays.asList(arr));
System.out.println("List: " + lis);
// Adding a new element to the ArrayList
lis.add(9);
// Printing the updated list
System.out.println("Updated List: " + lis);
}
}
Output
List: [1, 2, 3, 4, 5, 6, 7, 8] Updated List: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Explanation:
- Here, we first use
Arrays.asList()
to convert the array into a list. Then, we pass that list to theArrayList
constructor to create anArrayList
, which allows us to modify the list by adding new elements. - This method provides the flexibility of adding or removing elements from the list, which is not possible with
Arrays.asList()
alone.
Example 3: Using Stream
API (Java 8 and above)
If you are using the Java 8 or later, you can also use the Stream
API to convert an array into a list in a functional style.
import java.util.*;
import java.util.stream.*;
public class ArrayToList {
public static void main(String[] args) {
// array declaration
Double[] arr = {1.1, 2.2, 3.3, 4.4, 5.5};
// Converting array to list using Stream API
List<Double> lis = Arrays.stream(arr).collect(Collectors.toList());
System.out.println("List: " + lis);
}
}
Output
List: [1.1, 2.2, 3.3, 4.4, 5.5]
Explanation:
- The
Arrays.stream()
method creates a stream from the array, andCollectors.toList()
collects the elements of the stream into a list. - This is a clean and efficient way to convert an array to a list, especially when you want to apply additional operations on the stream before collecting the results.
Example 4: Using Collections.addAll()
Method
If you want to add elements of an array to an existing list, the Collections.addAll()
method is a great choice. It accepts the list and the array as parameters.
import java.util.*;
public class ArrayToList {
public static void main(String[] args) {
// Taking an array
String[] arr = {"Geeks", "forGeeks", "A computer Portal" };
// Creating an ArrayList and adding array elements to it
List<String> lis = new ArrayList<>();
Collections.addAll(lis, arr);
// Printing the list
System.out.println("List: " + lis);
}
}
Output
List: [Geeks, forGeeks, A computer Portal]
Example 5: Using List.of()
Method (Java 9+)
From Java 9 onwards, you can use the List.of()
method to create immutable lists directly from the array.
import java.util.*;
public class ArrayToList{
public static void main(String[] args) {
// taking an array
String[] arr = {"geeks", "for", "geeks"};
// Converting array to immutable list
List<String> list = List.of(arr);
System.out.println("List: " + list);
}
}
Output
List: [geeks, for, geeks]