Introduction
In Java 8, converting an array to a list is a common task when working with collections. While the traditional approach uses Arrays.asList()
, the Stream API offers a functional and flexible way to convert an array into a list using streams. By converting the array to a stream, you can easily perform additional operations like filtering, mapping, or sorting before collecting the elements into a list.
In this guide, we will learn how to convert an array to a list using Java 8's Stream API.
Solution Steps
- Define the Array: Create an array of elements (e.g., strings or integers) that you want to convert to a list.
- Convert the Array to a Stream: Use
Arrays.stream()
orStream.of()
to convert the array to a stream. - Collect the Stream to a List: Use the
collect(Collectors.toList())
method to collect the stream elements into a list. - Display the Result: Print or use the resulting list.
Java Program
Example 1: Convert an Array of Strings to a List
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ArrayToListUsingStream {
public static void main(String[] args) {
// Step 1: Define the array of strings
String[] fruits = {"Apple", "Banana", "Orange", "Mango"};
// Step 2: Convert the array to a Stream and collect to a List
List<String> fruitList = Arrays.stream(fruits)
.collect(Collectors.toList());
// Step 3: Display the List
System.out.println(fruitList);
}
}
Output
[Apple, Banana, Orange, Mango]
Explanation
Step 1: Define the Array
We define an array of strings:
String[] fruits = {"Apple", "Banana", "Orange", "Mango"};
Step 2: Convert the Array to a Stream and Collect to a List
We use Arrays.stream()
to convert the array into a stream, and then collect the stream elements into a list using Collectors.toList()
:
List<String> fruitList = Arrays.stream(fruits)
.collect(Collectors.toList());
Step 3: Display the List
We print the resulting list:
System.out.println(fruitList);
Example 2: Convert an Array of Integers to a List
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ArrayToListOfIntegers {
public static void main(String[] args) {
// Step 1: Define the array of integers
Integer[] numbers = {1, 2, 3, 4, 5};
// Step 2: Convert the array to a Stream and collect to a List
List<Integer> numberList = Arrays.stream(numbers)
.collect(Collectors.toList());
// Step 3: Display the List
System.out.println(numberList);
}
}
Output
[1, 2, 3, 4, 5]
Explanation
Step 1: Define the Array
We define an array of integers:
Integer[] numbers = {1, 2, 3, 4, 5};
Step 2: Convert the Array to a Stream and Collect to a List
We use Arrays.stream()
to convert the array to a stream and collect the elements into a list:
List<Integer> numberList = Arrays.stream(numbers)
.collect(Collectors.toList());
Step 3: Display the List
We print the resulting list:
System.out.println(numberList);
Example 3: Convert a Primitive Array to a List (Using boxed()
)
For primitive arrays like int[]
, you need to box the primitive values into their wrapper class (Integer
) before collecting them into a list.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class PrimitiveArrayToList {
public static void main(String[] args) {
// Step 1: Define the primitive int array
int[] numbers = {10, 20, 30, 40, 50};
// Step 2: Convert the primitive int array to a Stream and collect to a List
List<Integer> numberList = Arrays.stream(numbers)
.boxed() // Convert from int to Integer
.collect(Collectors.toList());
// Step 3: Display the List
System.out.println(numberList);
}
}
Output
[10, 20, 30, 40, 50]
Explanation
Step 1: Define the Primitive Array
We define a primitive int
array:
int[] numbers = {10, 20, 30, 40, 50};
Step 2: Convert the Array to a Stream and Box the Primitives
We use Arrays.stream()
to create an IntStream
from the primitive array. The boxed()
method converts the int
values to their wrapper class Integer
:
List<Integer> numberList = Arrays.stream(numbers)
.boxed()
.collect(Collectors.toList());
Step 3: Display the List
We print the resulting list:
System.out.println(numberList);
Conclusion
In Java 8, converting an array to a list is easily achieved using the Stream API. By using Arrays.stream()
or Stream.of()
, you can convert an array into a stream and collect the elements into a list using Collectors.toList()
. For primitive arrays, the boxed()
method is required to convert primitive types to their wrapper classes. This approach provides a flexible and efficient way to process arrays in a functional style.
Comments
Post a Comment
Leave Comment