Short-Circuiting Operations in Streams in java 8

1)  Program/ Example - Short Circuiting Operations in Streams in java 8

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
* Write Program to show - Short Circuiting Operations - in java 8
public class StreamsExample3_ShortCircuitingOperations {
   public static void main(String[] args) {
          List<String> StringList = new ArrayList<>();
          // In one line display all distinct records in stream
          System.out.println("\nUse stream for finding distinct elements and and display");
          Stream<String> stream =; // Convert list (ArrayList) of String to stream
          stream =;
                 .forEach(System.out::println);  // Displays -
          System.out.println("\n"+"SHORT-CIRCUITING operations - (terminal operations)");
/*         short-circuiting operations - (terminal operations)
          When infinite input is passed and finite stream is produced (in finite time).
          System.out.println("\n1.Short-circuiting stateful intermediate operations");
          //short-circuiting stateful intermediate operation
          stream =;
          //findFirst() method - returns first element in the stream
          System.out.println("findFirst() method >");
          Optional<String> optional1 = stream.findFirst();
          stream =;
          //findAny() method - returns any element in the stream
          System.out.println("stream.findAny() >");
          Optional<String> optional2 = stream.findAny();
          stream =;
          // In one line find whether any record start with 'a' in stream
          boolean anyRecordStartsWithA = stream.anyMatch((s) -> s.startsWith("a"));
          System.out.println("\n2.any Record Starts With 'a'  = "+anyRecordStartsWithA); //true
          stream =;
          // In one line find whether all records start with 'a' in stream
          boolean allRecordStartsWithA = stream.allMatch((s) -> s.startsWith("a"));
          System.out.println("\n3.all Record Starts With 'a'  = "+allRecordStartsWithA); //false
          stream =;
          // In one line find whether none of the records start with 'b' in stream
          boolean noneRecordStartsWithB = stream.noneMatch((s) -> s.startsWith("b"));
          System.out.println("\n4.none Record Starts With 'b' = "+ noneRecordStartsWithB); // true
          stream =; // Convert list (ArrayList) to stream
          // In one line count records start with 'a' in stream
          long countOfRecordsStartsWithA = stream.filter((s) -> s.startsWith("a")).count();
          System.out.println("\n5.count Records Starts With 'a' = "+countOfRecordsStartsWithA); // 3
Use stream for finding distinct elements and and display
SHORT-CIRCUITING operations - (terminal operations)
1.Short-circuiting stateful intermediate operations
findFirst() method >
stream.findAny() >
2.any Record Starts With 'a'  = true
3.all Record Starts With 'a'  = false
4.none Record Starts With 'b' = false
5.count Records Starts With 'a' = 2

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking (JMSE) on facebook, following on google+ or Twitter.

Must read for you :