Monday 17 July 2017

How to use iterate method of Java 8 Stream | Java 8 streams tutorial | Streams in Java 8


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

StreamDemo1.java
import java.util.stream.Stream;

/**
 * 
 * We can use stream to iterate any number of times. Stream provides
 * predefined methods to deal with the logic you implement.
 * 
 */
public class StreamDemo1
{
    public static void main(String[] args)
    {
        Stream.iterate(1, element -> element + 1)
                .forEach(System.out::println);
    }
}
Output
1
2
3
4
5
6
7
8
9
10
11
..
..
..
..
StreamDemo2.java
import java.util.stream.Stream;

/**
 * 
 * We can use stream to iterate any number of times. Stream provides
 * predefined methods to deal with the logic you implement. In the
 * following example, we are iterating, filtering and passed a limit
 * to fix the iteration.
 * 
 */
public class StreamDemo2
{
    public static void main(String[] args)
    {
        Stream.iterate(1, element -> element + 1)
                .filter(element -> element % 10 == 0).limit(5)
                .forEach(System.out::println);

    }
}
Output
10
20
30
40
50

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/28b768ba4f37f4b22418a8a2d2a1f53c7d0de625/BasicJava/StreamDemo_iterate_method_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