Monday 23 October 2017

How to use the Intermediate operations[match and reduce] of Java 8 Stream | Streams in Java 8


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=BAdEgMA96xo&list=UUhwKlOVR041tngjerWxVccw

StreamDemo1.java
import java.util.Arrays;
import java.util.List;

/**
 * Terminal operation : match()
 */
public class StreamDemo1
{
    public static void main(String[] args)
    {
        List<String> list = Arrays.asList("Ball", "Cat", "Dog", "Apple");

        /*
         * match() operation returns a boolean depending upon the condition
         * applied on stream data.
         */
        boolean result = list.stream().anyMatch((s) -> s.startsWith("D"));
        System.out.println(result);
    }
}
Output
true

StreamDemo2.java
import java.util.stream.IntStream;

/**
 * Terminal operation : reduce()
 */
public class StreamDemo2
{
    public static void main(String[] args)
    {
    
        /*
         * reduce() operation combines the stream elements into one using a
         * BinaryOperator.
         * 
         */
        int result = IntStream.of(1, 2, 3, 4).reduce(0, (a, b) -> a + b);
        System.out.println(result);
    }

}
Output
10

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_terminal_op_mr.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StreamDemo_terminal_op_mr

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/921a65d57c0ec8a07d5edae78f1552619b89e108/BasicJava/StreamDemo_terminal_op_mr/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment