Java 8 – How to Convert Array to Stream

Introduction

In Java 8, the Stream API provides an easy way to process data in a functional style. Converting an array to a stream allows you to use powerful stream operations such as filter(), map(), and collect(). Java provides two main methods for converting arrays into streams: Arrays.stream() and Stream.of().

In this guide, we will learn how to convert an array into a stream using Java 8.

Solution Steps

  1. Define the Array: Create an array of elements (e.g., strings or integers) that you want to convert to a stream.
  2. Convert Array to Stream Using Arrays.stream(): Use the Arrays.stream() method to convert the array to a stream.
  3. Convert Array to Stream Using Stream.of(): Alternatively, use Stream.of() to convert the array to a stream.
  4. Process the Stream: Apply operations like forEach(), filter(), or map() to process the stream.
  5. Display the Result: Print or collect the processed elements.

Java Program

Example 1: Using Arrays.stream()

import java.util.Arrays;
import java.util.stream.Stream;

public class ArrayToStreamUsingArraysStream {
    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 using Arrays.stream()
        Stream<String> fruitStream = Arrays.stream(fruits);

        // Step 3: Process and display each element in the stream
        fruitStream.forEach(System.out::println);
    }
}

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

We use the Arrays.stream() method to convert the array to a stream:

Stream<String> fruitStream = Arrays.stream(fruits);

Step 3: Process the Stream

We use forEach() to print each element of the stream:

fruitStream.forEach(System.out::println);

Example 2: Using Stream.of()

import java.util.stream.Stream;

public class ArrayToStreamUsingStreamOf {
    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 using Stream.of()
        Stream<Integer> numberStream = Stream.of(numbers);

        // Step 3: Process and 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 integers:

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:

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: Processing the Stream (Filtering Even Numbers)

You can also apply operations like filter() to process elements in the stream.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayToStreamWithProcessing {
    public static void main(String[] args) {
        // Step 1: Define an array of integers
        Integer[] numbers = {10, 15, 20, 25, 30, 35};

        // Step 2: Convert the array to a stream and filter even numbers
        List<Integer> evenNumbers = Arrays.stream(numbers)
                                          .filter(n -> n % 2 == 0)  // Filter even numbers
                                          .collect(Collectors.toList());  // Collect to List

        // Step 3: Display the filtered list of even numbers
        System.out.println(evenNumbers);
    }
}

Output

[10, 20, 30]

Explanation

Step 1: Define the Array

We define an array of integers:

Integer[] numbers = {10, 15, 20, 25, 30, 35};

Step 2: Convert the Array to a Stream and Filter Even Numbers

We convert the array into a stream using Arrays.stream(). The filter() method is used to retain only even numbers, and the collect() method gathers the results into a list:

List<Integer> evenNumbers = Arrays.stream(numbers)
                                  .filter(n -> n % 2 == 0)
                                  .collect(Collectors.toList());

Step 3: Display the Result

The filtered list of even numbers is printed:

System.out.println(evenNumbers);

Conclusion

In Java 8, converting an array to a stream is simple and can be done using either Arrays.stream() or Stream.of(). Once converted to a stream, you can easily process the elements using stream operations such as filter(), map(), and collect(). This makes working with arrays more functional and flexible.

Comments