Stream Creating Methods

Java-Stream Creating Methods

Stream Creating Methods

You can turn any collection into a stream with the stream method of the Collection interface, but if you have an array, use the static Stream.of method instead. This method has three forms:

Stream Creating Methods

The second form has a varargs parameter, so you can construct a stream from any number of arguments.

Using Arrays.stream( ) Method to Create a Stream

A stream can also be obtained from an array by use of the static stream( ) method, which was added to the Arrays class. Two of its forms is:

Stream Creating Methods

Use Arrays.stream(T[] ts, int i, int i1) to make a stream from array elements between positions from (inclusive) and to (exclusive).


Using Pattern class splitAsStream Method to Create a String Stream

The Pattern class has a method splitAsStream that returns the stream of strings computed by splitting the input around matches of this pattern.

Stream Creating Methods

Making Infinite IntStream

The Stream interface has two static methods for making infinite Streams. The generate( ) and iterate( ). The generate method takes a function with no arguments (or, technically, an object of the Supplier interface). Whenever a stream value is needed, that function is called to produce a value. To produce infinite sequences, such as 0 1 2 3 . . . , use the iterate method. It takes a “seed” value and a function (technically, a UnaryOperator<T>) and repeatedly applies the function to the previous result. The iterate( ) has two forms:

Stream Creating Methods 8

The limit() and skip() Methods

The call stream.limit(n) returns a new stream that ends after n elements (or when the original stream ends, if it is shorter). This method is particularly useful for cutting infinite streams down to size.

Stream Creating Methods

The call of instance method skip() returns a stream consisting of the remaining elements of this stream after discarding the first specified number of elements of the stream.

Stream Creating Methods

Making a Empty Stream

To make a stream with no elements, use the static Stream.empty( ) method.

Program

Stream Creating Methods

Program Source

import java.util.stream.Stream;
import java.util.Arrays;
public class Javaapp {

    public static void main(String[] args) {
        
        Integer inary[] = {10,20,30,40,50};
        Stream<Integer> istm1 = Stream.of(inary);
        System.out.print("istm1--> ");
        istm1.forEach((e)->System.out.print(e+","));
        System.out.println();
        

        String strs[] = {"One","Two","Three","Four","Five"};
        Stream<String> sstm1 = Arrays.stream(strs);
        System.out.print("sstm1--> ");
        sstm1.forEach((e)->System.out.print(e+","));
        System.out.println();
        
        
        Stream<Integer> istm2 = Stream.iterate(5, (e)->e<100, (e)->e+=5);
        System.out.print("istm2--> ");
        istm2.forEach((e)->System.out.print(e+","));
        
    }
}

Leave a Comment