Thursday, 10 August 2017

How to convert Stream to Array using toArray method of Java 8 Stream | Streams in Java 8


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

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

/**
 *
 * Convert Stream to array using stream.toArray(EntryType[]::new)
 *
 */
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();
        Integer[] evenNumbersArr = stream.filter(i -> i % 2 == 0)
                                         .toArray(Integer[]::new);

        for (Integer evenNumber : evenNumbersArr)
        {
            System.out.println(evenNumber);
        }
    }
}
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_StreamToArr_App.zip?attredirects=0&d=1

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

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

    1. very very amazing explaintion....many things gather about yourself...yes realy i enjoy it
      SEO Company in India

      ReplyDelete

    Google+ Followers

    Google+ Badge