Thursday, 10 August 2017

How to convert Stream to List using collect method of Java 8 Stream | Streams in Java 8


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

StreamDemo.java
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import 
java.util.stream.Stream;

/**
 * 
 * Convert Stream to List using stream.collect(Collectors.toList())
 *
 */
public class StreamDemo
{
    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 1; i < 20; i++)
        {
            list.add(i);
        }
        Stream<Integer> stream = list.stream();

        List<Integer> evenNumbersList = stream.filter(i -> i % 2 == 0)
                                              .collect(Collectors.toList());

        System.out.print(evenNumbersList);
    }
}
Output
[2, 4, 6, 8, 10, 12, 14, 16, 18]

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

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

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

    Google+ Followers

    Google+ Badge