Thursday 10 August 2017

What are the different ways we can create the streams | Java 8 streams | Streams in Java 8


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

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

/**
 * 
 * How to create stream Using Stream.of(val1, val2, val3….)
 */

public class StreamDemo1
{
    public static void main(String[] args)
    {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
        stream.forEach(p -> System.out.println(p));
    }
}
Output
1
2
3
4
5
6
7
8
9
StreamDemo2.java
import java.util.stream.Stream;

/**
 * 
 * How to create stream Using Stream.of(arrayOfElements)
 */

public class StreamDemo2
{
    public static void main(String[] args)
    {
        Integer[] integerArray = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        Stream<Integer> stream = Stream.of(integerArray);
        stream.forEach(p -> System.out.println(p));
    }
}
Output
1
2
3
4
5
6
7
8
9
StreamDemo3.java
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * 
 * How to create stream Using someList.stream()
 */

public class StreamDemo3
{
    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 1; i < 10; i++)
        {
            list.add(i);
        }
        Stream<Integer> stream = list.stream();
        stream.forEach(p -> System.out.println(p));
    }
}
Output
1
2
3
4
5
6
7
8
9
StreamDemo4.java
import java.util.Date;
import java.util.stream.Stream;

/**
 * 
 * How to create stream Using Stream.generate() or Stream.iterate()
 * functions
 */

public class StreamDemo4
{
    public static void main(String[] args)
    {
        Stream<Date> stream = Stream.generate(() -> {return new Date(); });
        stream.forEach(p -> System.out.println(p));
    }
}
Output
Sun Jul 16 09:18:50 IST 2017
Sun Jul 16 09:18:50 IST 2017
Sun Jul 16 09:18:50 IST 2017
Sun Jul 16 09:18:50 IST 2017
Sun Jul 16 09:18:50 IST 2017
Sun Jul 16 09:18:50 IST 2017
Sun Jul 16 09:18:50 IST 2017
Sun Jul 16 09:18:50 IST 2017
Sun Jul 16 09:18:50 IST 2017
---
---
---
StreamDemo5.java
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * 
 * How to create stream Using String chars or String tokens
 */

public class StreamDemo5
{
    public static void main(String[] args)
    {
        IntStream intStream = "12345_abcdefg".chars();
        intStream.forEach(p -> System.out.println(p));

        System.out.println("\n-------------------------\n");

        Stream<String> stream = Stream.of("A$B$C".split("\\$"));
        stream.forEach(p -> System.out.println(p));
    }
}
Output
49
50
51
52
53
95
97
98
99
100
101
102
103

-------------------------

A
B
C

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

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

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