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
Program to Convert Stream to an Array in Java A Stream is a sequence of objects that support various methods which can be pipelined to produce the desired result. An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition
3 min read
Java Program to Convert Integer List to Integer Array There are many ways to convert integer List to ArrayList where in this article we will be discussing out 2 approaches as below: Using concept od streams in Java8Using Apache Commons LangUsing Guava Library Method 1: Using concept od streams in Java8 So, first, we will get to know about how to conver
3 min read
Program to convert Array to Set in Java Array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In the case of primitives data types, the actual values are stored in contiguous memory locations. In cas
7 min read
Program to Convert Set to List in Java Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be s
4 min read
Program to Convert a Vector to List in Java Given a Vector, the task is to Convert Vector to List in Java Examples: Input: Vector: [1, 2, 3, 4, 5] Output: List: [1, 2, 3, 4, 5] Input : Vector = [a, b, c, d, e, f] Output : List = [a, b, c, d, e, f] Using Collections.list() method Syntax: List list = Collections.list(vec.elements()); Approach:
3 min read
How to Convert a PriorityQueue into an Array, List, and Set in Java? In Java, PriorityQueue is an implementation of the Queue interface. It means high-priority elements can be saved first compared to lower-priority elements in the queue, and it follows the natural order of elements. In this article, we will learn how to convert a PriorityQueue into an Array, List, an
3 min read
Java Program to Change a Collection to an Array An array is a data structure that can hold a fixed-size, homogeneous collection of elements of the same data type, which can be either primitive data types (e.g., int, float) or object references. However, the size of the array cannot be changed once it is created. On the other hand, a collection is
3 min read
How to Convert an ArrayList into a JSON String in Java? In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList into a JSON string in Java. Steps to convert an ArrayList into a JSON str
2 min read
Program to Convert List to Stream 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
3 min read
Program to convert Primitive Array to Stream in Java An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case
3 min read