java tutorial: 5 ways to create stream in java 8

Stream API is a new feature introduced by Java 8 for processing collections of objects.

A stream is a sequence of objects, which supports many different methods, which can be arranged, filtered, and transformed through a pipeline to produce the desired result.

Note that when using streams:

  1. A stream itself does not store elements;
  2. A stream will not change the original data object;
  3. Stream operations are executed lazily, and are executed only when the result is required to return.

This article introduces you to common ways to create streams.

Create stream from collection

Steps:

  1. Create a collection
  2. Use Collection.stream() method to construct sequence stream from collection
  3. Print stream

Code:

package com.example.java8.stream;

import java.util.ArrayList;
import java.util.stream.Stream;

public class CreateStreamFromCollection {
    public static void main(String[] args) {
        ArrayList<String> stringArrayList = new ArrayList<>();
        stringArrayList.add("Hello");
        stringArrayList.add("Lambda");
        stringArrayList.add("Tom");
        stringArrayList.add("Generic");

        Stream<String> stream = stringArrayList.stream();
        stream.forEach(System.out::println);
    }
}

Create stream from array

Steps:

  1. Create an array
  2. Use the Arrays.stream() method to return a stream with the specified array as the source
  3. Print stream

Code:

package com.example.java8.stream;

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

public class CreateStreamFromArray {
    public static void main(String[] args) {
        Integer[] integers = {100,23,31,21,87,46};
        Stream<Integer> stream = Arrays.stream(integers);
        stream.forEach((x)->{
            System.out.println(x);
        });
    }
}

Create a stream from specified values

Stream.of(T…t) method can be used to create a stream with the specified t values, where t are the elements. This method returns a sequential Stream containing the t elements.

Code:

package com.example.java8.stream;

import java.util.stream.Stream;

public class CreateStreamFromSpecifiedValue {
    public static void main(String[] args) {
        Stream<String> a = Stream.of("a", "b", "c", "d");
        a.forEach(System.out::println);
    }
}

Create an infinite stream using Stream.generate() method

Stream.generate() method, returns an infinite sequential unordered stream where each element is generated by the provided Supplier Function Interface.
This is suitable for generating constant streams, streams of random elements, etc.

Code:

package com.example.java8.stream;

import java.util.stream.Stream;

public class CreateStreamFromGenerate {
    public static void main(String[] args) {
        Stream<Double> generate = Stream.generate(() -> {
            return Math.random();
        });
        generate.limit(5).forEach(System.out::println);
    }
}

Create an infinite stream using Stream.iterate() method

Stream.iterate() method returns an infinite-length Stream. Unlike the generate method, it generates an infinite continuous and ordered Stream by iterating the specified element seed through the function f.

Code:

package com.example.java8.stream;

import java.util.stream.Stream;

public class CreateStreamFromIterate {
    public static void main(String[] args) {
        Stream<Integer> iterate = Stream.iterate(2, (x)->{return x + 2;});
        iterate.limit(5).forEach(System.out::println);
    }
}

Add a Comment

Your email address will not be published.