Tuesday 19 September 2017

How to use range and reduce method of IntStream | Java 8 streams | Streams in Java 8


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

StreamDemo.java
import java.util.OptionalInt;
import java.util.stream.IntStream;

public class StreamDemo
{
    public static void main(String[] args)
    {
        /*
         * sum = 6 (1 + 2 + 3)
         */
        OptionalInt optionalInt = IntStream.range(1, 4).reduce((a, b) -> a + b);
        
        System.out.println(optionalInt.getAsInt());
        
        /*
         * sum = 16 (10 + 1 + 2 + 3)
         */
        int sum = IntStream.range(1, 4).reduce(10, (a, b) -> a + b);
        
        System.out.println(sum);
    }

}
Output
6
16

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

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

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