Tuesday 17 October 2017

How to generate stream from arrays and collections | Java 8 streams | Streams in Java 8


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

StreamDemo1.java
import java.util.Arrays;
import java.util.stream.Stream;
/**
 * Generate Streams from Arrays using .stream or Stream.of.
 */
public class StreamDemo1
{
    public static void main(String[] args)
    {
        String[] strArray = { "Apple", "Ball", "Cat", "Dog", "Eagle" };

        // Get Stream using the Arrays.stream
        Stream<String> stream1 = Arrays.stream(strArray);
        stream1.forEach(x -> System.out.println(x));

        System.out.println("---------------------------------");
        
        // Get Stream using the Stream.of
        Stream<String> stream2 = Stream.of(strArray);
        stream2.forEach(x -> System.out.println(x));
    }

}
Output
Apple
Ball
Cat
Dog
Eagle
---------------------------------
Apple
Ball
Cat
Dog
Eagle

StreamDemo2.java
import java.util.ArrayList;
import java.util.List;
/**
 * Generate Streams from Collections.
 */
public class StreamDemo2
{
    public static void main(String[] args)
    {
        List<String> itemList = new ArrayList<>();
        itemList.add("Apple");
        itemList.add("Ball");
        itemList.add("Cat");
        itemList.add("Dog");

        itemList.stream().forEach(item -> System.out.println(item));
    }

}
Output
Apple
Ball
Cat
Dog

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

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

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