Streams Creation

Java 8 introduced the Stream API, providing a powerful and flexible way to process sequences of elements. Streams can be created from various sources, each suited to different scenarios.

Here’s a comprehensive look at how to create streams:

From Collections:

Collections are the most common source of streams. Both List and Set can create streams.

import java.util.*;
import java.util.stream.*;

public class StreamFromCollectionExample {
    public static void main(String[] args) {
        // Creating a collection (List) of names
        List<String> names = Arrays.asList("LotusJavaPrince", "Mahesh", "Aarav", "Alok", "Amit");

        // Creating a stream from the collection
        Stream<String> nameStream = names.stream();

        // Performing operations using stream
        System.out.println("Names starting with 'A' in uppercase:");
        nameStream
            .filter(name -> name.startsWith("A"))       // Filter names that start with "A"
            .map(String::toUpperCase)                   // Convert them to uppercase
            .forEach(System.out::println);              // Print each name
    }
}

From Arrays

Arrays can also be a source for streams using the Arrays.stream() method.

import java.util.stream.*;

public class StreamFromArrayExample {
    public static void main(String[] args) {
        // Creating an array of names
        String[] names = { "LotusJavaPrince", "Mahesh", "Aarav", "Alok", "Amit" };

        // Creating a stream from the array
        Stream<String> nameStream = Arrays.stream(names);

        // Performing operations using stream
        System.out.println("Names starting with 'A' in uppercase:");
        nameStream
            .filter(name -> name.startsWith("A"))       // Filter names that start with "A"
            .map(String::toUpperCase)                   // Convert them to uppercase
            .forEach(System.out::println);              // Print each name
    }
}

From Static Methods:

The Stream class provides several static methods for creating streams.

  • of() Method: Creates a stream from a specified list of elements.
  • generate() Method: Creates an infinite stream where each element is generated by a provided Supplier.
  • iterate() Method: Creates an infinite stream where each element is produced by applying a function to the previous element.
import java.util.stream.Stream;

public class StaticStreamCreation {

    // Using Stream.of() - Creates a finite stream from given values
    public static void streamOfValues() {
        System.out.println("Stream.of() output:");
        Stream<String> streamFromValues = Stream.of("Paani", "Mahesh", "Datta", "Ganesh", "Harsha");
        streamFromValues.forEach(System.out::println);
        System.out.println();
    }

    // Using Stream.generate() - Generates an infinite stream (limited here)
    public static void streamGenerate() {
        System.out.println("Stream.generate() output:");
        Stream<String> generatedStream = Stream.generate(() -> "Hello").limit(5);
        generatedStream.forEach(System.out::println);
        System.out.println();
    }

    // Using Stream.iterate() - Creates a stream by applying function to previous element
    public static void streamIterate() {
        System.out.println("Stream.iterate() output:");
        Stream<Integer> iteratedStream = Stream.iterate(0, n -> n + 2).limit(5);
        iteratedStream.forEach(System.out::println);
        System.out.println();
    }

    public static void main(String[] args) {
        // Call each static method
        streamOfValues();
        streamGenerate();
        streamIterate();
    }
}

Creating streams in Java is versatile and straightforward, with multiple ways to derive streams from various data sources. Understanding these methods enhances the ability to leverage the full power of the Stream API for efficient and expressive data processing.

Scroll to Top