Thursday 24 August 2017

Terminal operations[reduce] in Java 8 Stream | Streams in Java 8


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

Click the below Image to Enlarge
Terminal operations[reduce] in Java 8 Stream | Streams in Java 8 
StreamReduceDemo.java
import java.util.Arrays;
import java.util.List;

public class StreamReduceDemo
{
    public static void main(String[] args)
    {
        List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        /*
         * reduce processes all elements of the stream and produces a
         * single result.
         */
        numberList.stream().reduce((i1, i2) -> i1 > i2 ? i1 : i2)
                .ifPresent(i -> System.out.println("max: " + i));

        Integer total = numberList.stream().reduce(0,(i1, i2) -> i1 + i2);

        System.out.println("total: " + total);
    }
}
Output
max: 9
total: 45

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/21c94b97f3d45cf2018afb81344804fbe9e1995b/BasicJava/StreamDemo_Ter_Op_reduce_App/?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