Part 8 – Short circuit operations in Java 8

Short circuit operations in Java 8

What have we learned so far

1. Functional Interface And Default Methods In Java 8 –
PART 1 : FUNCTIONAL INTERFACE AND DEFAULT METHODS IN JAVA 8

2. Lambda Expression in Java 8
PART 2 – LAMBDA EXPRESSION IN JAVA 8

3. Method Reference in Java 8
PART 3 – METHOD REFERENCE IN JAVA 8

4. Optional in Java 8
PART 4 – OPTIONAL IN JAVA 8

5. filter(), findAny() in Java 8
PART 5 – FILTER(), FINDANY() IN JAVA 8

6. map() vs flatMap() in Java 8
PART 6 – MAP() VS FLATMAP() IN JAVA 8

7. Stream.peek() in Java 8
PART 7 – STREAM.PEEK() IN JAVA 8

 

Short circuiting operations in Java 8

Short-circuit operations are same as Boolean short circuiting in java, where the second condition is executed or evaluated only if the first argument does not suffice to determine the value of the expression. For example:

If(str != null && str.length>10)

In this condition, if we pass str as null then it will only execute the first condition and evaluate the result. The same concept of short circuiting java 8 uses in Stream.

Different Short circuiting operations in Java 8

1. limit() in Java 8

it limits the stream elements to process, In below example we are printing only first 5 elements from the stream.

int [] arr = {1,2,3,4,5,6,7,8,9,11};
  
System.out.println("*** Printing the first 5 elements of stream :");
Arrays.stream(arr)
      .limit(5)
      .forEach(System.out::println);

Output :
*** Printing the first 5 elements of stream :
1
2
3
4
5

2. findFirst(), findAny() in Java 8

firstFist() will return the first element from the Stream where as findAny returns any element from the Stream. findAny may give different result everytime.

int [] arr = {1,2,3,4,5,6,7,8,9};
  
Integer findFirst = Arrays.stream(arr).filter(i -> (i%2) == 0)
  .findFirst()
  .orElse(-1);

System.out.println("findFirst : " + findFirst);

Integer findAny = Arrays.stream(arr).filter(i -> (i%2) == 0)
  .findAny()
  .orElse(-1);

System.out.println("findAny : " + findAny);

Output :
findFirst : 2
findAny : 6

3. allMatch() anyMatch() noneMatch() in Java 8

These short circuit operators returns true or false value depending on the condition being evaluated.allMatch() – this will evaluate the condition for all the steams and will return a boolean value.
anyMatch() – this will evaluate the condition in the stream until it finds its match and once it finds the match, It will exit the processing and retruns the rboolean value.
noneMatch() – This is exact opposite of allMatch()
Lets see their behavior through some practical example.

int [] arr = {1,2,3,4,5,6,7,8,9,11};
    
System.out.println("All numbers are less than 12 : " + Arrays.stream(arr).allMatch(i-> i < 12));
System.out.println("Contains any numbers greater than 10 : " + Arrays.stream(arr).anyMatch(i-> i == 8));
System.out.println("All numbers are less than 10 : " + Arrays.stream(arr).noneMatch(i-> i > 10));
Output :
All numbers are less than 12 : true
Contains any numbers greater than 10 : true
All numbers are less than 10 : false

Source Code
Download source code of Java 8 features from below git repository :
java-8-features

Java 8 Features Tutorial

https://www.onlyfullstack.com/java-8-features-tutorial/



Lets go to our next tutorial where we will discuss below points :

Advance Collectors in Java 8
In this tutorial we will understand below topics – joining() in Java 8
 – summaryStatistics() in Java 8
 – partitioningBy() in Java 8
 – groupingBy() in Java 8
 – mappingBy() in Java 8
PART 9 – ADVANCE COLLECTORS IN JAVA 8