Wednesday, October 11, 2017

Streams in Java 8

1. A stream does not store its elements. They may be stored in an underlying collection or generated on demand.
2. Stream operations don’t mutate their source. Instead, they return new streams.
that hold the result.
3. Stream operations are lazy when possible. This means they are not executed until their result is needed. For example, if you only ask for the first five long words instead of counting them all, then the filter method will stop filtering after the fifth match. As a consequence, you can even have infinite streams.

When you work with streams, you set up a pipeline of operations in three stages.
1. You create a stream.
2. You specify intermediate operations for transforming the initial stream into others, in one or more steps.
3. You apply a terminal operation to produce a result. This operation forces the execution of the lazy operations that precede it. Afterwards, the stream can no longer be used.

NOTE:  Stream operations are not executed on the elements in the order in
which they are invoked on the streams. In our example, nothing happens until count is called. When the count method asks for the first element, then the filter method starts requesting elements, until it finds one that has length > 12.

Working with files

try (Stream<String> lines = Files.lines(path)) {
Do something with lines
}

Below program focus on Stream Creations from different sources:


package com.collection;

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

public class StreamCreation {
     public static void main(String[] args) {
          String[] tokens = "Its going to be a great Wednesday".split(" ");

          /* Convert an array into Stream */
          Stream<String> fullStream = Stream.of(tokens);
          System.out.println("Arrays to Stream->"+fullStream.count()+"\n"+"******");
         
          /* Convert an array into partial Stream */
          Stream<String> partialStream = Arrays.stream(tokens, 1, 4);
          System.out.println("Arrays to partial Stream->"+partialStream.count()+"\n"+"******");
         
          /* Returns an empty sequential Stream */
          Stream<String> silenceStream = Stream.empty();
          System.out.println("Silence Stream->"+silenceStream.count()+"\n"+"******");

          /* Returns an infinite sequential unordered stream where
           * each element is generated by the provided Supplier
           * Supplier Interface
          */
          System.out.println("Generate Infinite Stream->");
          Stream<String> echos = Stream.generate(() -> "Echo").limit(5);
          echos.forEach(System.out::println);
         
          Stream<Double> randoms = Stream.generate(()->Math.random()).limit(5);
          System.out.println(randoms.count()+"\n"+"******");
         
          /* Returns an infinite sequential ordered Stream produced by iterative application of a
           * function f to an initial element seed, producing a Stream consisting of seed, f(seed),
           * f(f(seed)), etc.
          *  Parameters: seed the initial element
          *  f a function to be applied to to the previous element to produce a new element*/
          Stream<BigInteger> integers=Stream.iterate(BigInteger.ONE,n->n.add(BigInteger.ONE)).limit(5);
          integers.forEach(System.out::println);
     }
}

Output

Arrays to Stream->7
******
Arrays to partial Stream->3
******
Silence Stream->0
******
Generate Infinite Stream->
Echo
Echo
Echo
Echo
Echo
5
******
1
2
3
4

5

No comments:

Post a Comment