Introduction
In Java 8, converting an array of integers to a stream allows you to process the array elements using the Stream API's functional operations such as map()
, filter()
, and collect()
. The Stream API provides an efficient way to manipulate collections or arrays in a more readable and concise manner. To convert an array of integers into a stream, Java offers two common methods: Arrays.stream()
and Stream.of()
.
In this guide, we will explore both methods to convert an array of integers to a stream and demonstrate how to apply some common stream operations.
Solution Steps
- Define the Array: Create an array of integers to be processed.
- Convert Array to Stream Using
Arrays.stream()
: Use theArrays.stream()
method to convert the array to anIntStream
. - Convert Array to Stream Using
Stream.of()
: Alternatively, useStream.of()
to convert the array into aStream<Integer>
. - Process the Stream: Apply operations like
filter()
,map()
, orcollect()
on the stream. - Display or Collect the Result: Print or collect the processed elements.
Java Program
Example 1: Using Arrays.stream()
for Primitive Int Array
import java.util.Arrays;
import java.util.stream.IntStream;
public class ArrayToIntStreamExample {
public static void main(String[] args) {
// Step 1: Define an array of primitive integers
int[] numbers = {1, 2, 3, 4, 5};
// Step 2: Convert the array to an IntStream using Arrays.stream()
IntStream numberStream = Arrays.stream(numbers);
// Step 3: Display each element in the stream
numberStream.forEach(System.out::println);
}
}
Output
1
2
3
4
5
Explanation
Step 1: Define the Array
We define a primitive integer array:
int[] numbers = {1, 2, 3, 4, 5};
Step 2: Convert the Array to an IntStream
We use Arrays.stream()
to convert the array into an IntStream
:
IntStream numberStream = Arrays.stream(numbers);
Step 3: Process the Stream
We use forEach()
to print each element in the stream:
numberStream.forEach(System.out::println);
Example 2: Using Stream.of()
for Wrapper Integer Array
import java.util.stream.Stream;
public class ArrayToStreamUsingStreamOf {
public static void main(String[] args) {
// Step 1: Define an array of Integer objects (Wrapper class)
Integer[] numbers = {1, 2, 3, 4, 5};
// Step 2: Convert the array to a Stream using Stream.of()
Stream<Integer> numberStream = Stream.of(numbers);
// Step 3: Display each element in the stream
numberStream.forEach(System.out::println);
}
}
Output
1
2
3
4
5
Explanation
Step 1: Define the Array
We define an array of Integer
objects (wrapper class for int
):
Integer[] numbers = {1, 2, 3, 4, 5};
Step 2: Convert the Array to a Stream
We use Stream.of()
to convert the array into a stream of Integer
objects:
Stream<Integer> numberStream = Stream.of(numbers);
Step 3: Process the Stream
We use forEach()
to print each element in the stream:
numberStream.forEach(System.out::println);
Example 3: Filtering Even Numbers Using Stream
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class FilterEvenNumbersFromArray {
public static void main(String[] args) {
// Step 1: Define an array of integers
int[] numbers = {10, 15, 20, 25, 30, 35};
// Step 2: Convert the array to an IntStream and filter even numbers
List<Integer> evenNumbers = Arrays.stream(numbers) // Convert to IntStream
.filter(n -> n % 2 == 0) // Filter even numbers
.boxed() // Convert from int to Integer
.collect(Collectors.toList()); // Collect to List
// Step 3: Display the result
System.out.println(evenNumbers);
}
}
Output
[10, 20, 30]
Explanation
Step 1: Define the Array
We define an array of integers:
int[] numbers = {10, 15, 20, 25, 30, 35};
Step 2: Convert the Array to an IntStream and Filter Even Numbers
We use Arrays.stream()
to convert the array to an IntStream
. Then, we apply filter()
to retain only even numbers. The boxed()
method converts the primitive int
values to Integer
objects, and collect()
gathers the result into a list:
List<Integer> evenNumbers = Arrays.stream(numbers)
.filter(n -> n % 2 == 0)
.boxed()
.collect(Collectors.toList());
Step 3: Display the Result
The list of even numbers is printed:
System.out.println(evenNumbers);
Conclusion
In Java 8, converting an array of integers into a stream can be done using either Arrays.stream()
for primitive int arrays or Stream.of()
for wrapper Integer
arrays. Once converted into a stream, you can leverage powerful operations like filtering, mapping, and collecting. This allows for more readable and functional code when working with arrays.
Comments
Post a Comment
Leave Comment