Thursday, September 11, 2014

Java 8 : Streams Creation

import java.math.BigInteger;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class StreamCreation {
       @SuppressWarnings(value = { "all" })
       public static void main(String[] args) {
              String contents = "public class StreamCreation {public static void main(String[] args) {String contents = \"\";}}";
              Stream words = Stream.of(contents.split("[\\P{L}]+"));
              words.forEach(System.out::println);
              Stream songs = Stream.of("gently", "down", "the", "stream");
              songs.forEach(System.out::println);
              Stream silence = Stream.empty();
              silence.forEach(System.out::println);
              Stream echos = Stream.generate(() -> "Echo");
              //echos.forEach(System.out::println);//commented as this are unlimited
              Stream randoms = Stream.generate(Math::random);
              //randoms.forEach(System.out::println);//commented as this are unlimited
              Stream integers = Stream.iterate(BigInteger.ZERO, n -> n.add(BigInteger.ONE));
              //integers.forEach(System.out::println);//commented as this are unlimited
              Stream wordStream = Pattern.compile("[\\P{L}]+").splitAsStream(contents);
              //using limit on unlimited streams.
              Stream.iterate(BigInteger.ZERO, n -> n.add(BigInteger.ONE)).limit(10).filter(n-> n.doubleValue()%2==0).forEach(System.out::println);
       }

}

No comments:

Post a Comment