Wednesday, 19 July 2017

How to convert List to stream and Array to Stream | Java 8 streams | Streams in Java 8


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

Click the below Image to Enlarge
How to convert List to stream and Array to Stream | Java 8 streams | Streams in Java 8 
StreamDemo1.java
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;


public class StreamDemo1
{
    public static void main(String[] args)
    {
        // Create an ArrayList
        List<Integer> numberList = new ArrayList<Integer>();
        numberList.add(1);
        numberList.add(5);
        numberList.add(8);

        // Convert it into a Stream
        Stream<Integer> stream = numberList.stream();
        System.out.println(stream);
        System.out.println("Count =  "+stream.count());

    }
}
Output
java.util.stream.ReferencePipeline$Head@6d06d69c
Count =  3
StreamDemo2.java
import java.util.Arrays;
import java.util.stream.Stream;

public class StreamDemo2
{
    public static void main(String[] args)
    {
        // Create an array
        Integer[] numberArray ={ 1, 5, 8, 10 };

        // Convert it into a Stream
        Stream<Integer> stream = Arrays.stream(numberArray);
        System.out.println(stream);
        System.out.println("Count =  "+stream.count());

    }
}
Output
java.util.stream.ReferencePipeline$Head@6d06d69c
Count =  4

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

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

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