Create a Stream from an Array in Java

1. Introduction

Creating a stream from an array is a common operation in Java, and it is particularly useful when you need to apply a series of stream operations to a collection of objects stored in an array. This operation enables the use of the Stream API's powerful data processing capabilities, such as filtering, mapping, and reduction.

Key Points

1. Arrays.stream() is a convenient method to create a stream from an array.

2. The method can be used to stream the entire array or a specific range of elements.

3. Stream.of() provides another way to create a stream directly from array elements.

4. Streams are versatile and can be created from collections using Collection.stream().

2. Program Steps

1. Import necessary classes.

2. Declare an array.

3. Use different methods to create streams from the array.

4. Apply stream operations to display elements.

3. Code Program

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

public class Main {

    public static void main(String[] args) {

        // Step 2: Declare an array
        String[] arr = {"One", "Two", "Three", "Four", "Five"};

        // Step 3a: Use Arrays.stream to create a stream from the entire array
        Stream<String> stream1 = Arrays.stream(arr);
        stream1.forEach(System.out::println);

        // Step 3b: Use Arrays.stream to create a stream from a range of the array
        Stream<String> stream2 = Arrays.stream(arr, 0, 2);
        stream2.forEach(System.out::println);

        // Step 3c: Use Collection.stream to create a stream from an array converted to a list
        Stream<String> stream3 = Arrays.asList(arr).stream();
        stream3.forEach(System.out::println);

        // Step 3d: Use Collection.stream on a sublist of the array converted to a list
        Stream<String> stream4 = Arrays.asList(arr).subList(0, 2).stream();
        stream4.forEach(System.out::println);

        // Step 3e: Use Stream.of to create a stream directly from the array
        Stream<String> stream5 = Stream.of(arr);
        stream5.forEach(System.out::println);

        // Step 3f: Use Stream.of with individual elements
        Stream<String> stream6 = Stream.of("One", "Two", "Three");
        stream6.forEach(System.out::println);
    }
}

Output:

One
Two
Three
Four
Five
One
Two
One
Two
Three
Four
Five
One
Two
One
Two
Three
Four
Five
One
Two
Three

Explanation:

1. Stream Creation: Different methods are demonstrated for creating streams from an array. Arrays.stream(arr) streams the entire array, while Arrays.stream(arr, 0, 2) focuses on elements from index 0 to 1.

2. Streaming from Lists: Converting the array to a list with Arrays.asList(arr) and then calling stream() or subList(0, 2).stream() shows how to work with a portion of the array or the entire array when it's wrapped in a list.

3. Direct Stream Creation: Stream.of(arr) and Stream.of("One", "Two", "Three") directly create streams either from an array or by listing elements.

4. Utility: These examples highlight the flexibility and utility of streams in processing collections, from simple print operations to potentially more complex data manipulation tasks.


Comments