Java Program to Convert an Array into a List
Last Updated :
24 Dec, 2024
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.
Java
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);
}
}
OutputList: [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.
Java
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);
}
}
OutputList: [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 the ArrayList
constructor to create an ArrayList
, 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.
Java
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);
}
}
OutputList: [1.1, 2.2, 3.3, 4.4, 5.5]
Explanation:
- The
Arrays.stream()
method creates a stream from the array, and Collectors.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.
Java
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);
}
}
OutputList: [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.
Java
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);
}
}
OutputList: [geeks, for, geeks]
Similar Reads
Convert List to Array in Java The List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects where duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. Now here we are give
5 min read
Program to Convert List to Map in Java The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack class
5 min read
Java Program to Convert ArrayList to LinkedList Given an array list, your task is to write a program to convert the given array list to Linked List in Java. Examples: Input: ArrayList: [Geeks, forGeeks, A computer Portal] Output: LinkedList: [Geeks, forGeeks, A computer Portal] Input: ArrayList: [1, 2, 3, 4, 5] Output: LinkedList: [1, 2, 3, 4, 5]
6 min read
Convert a String to a List of Characters in Java In Java, to convert a string into a list of characters, we can use several methods depending on the requirements. In this article, we will learn how to convert a string to a list of characters in Java.Example:In this example, we will use the toCharArray() method to convert a String into a character
3 min read
How to convert LinkedList to Array in Java? Given a Linked List in Java, the task is to convert this LinkedList to Array. Examples: Input: LinkedList: ['G', 'e', 'e', 'k', 's'] Output: Array: ['G', 'e', 'e', 'k', 's'] Input: LinkedList: [1, 2, 3, 4, 5] Output: Array: [1, 2, 3, 4, 5] Approach: Get the LinkedListConvert the LinkedList to Object
2 min read