How to create a Stream in Java 8?

There are many ways to create a stream instance of different sources.

Here is a some example to explain how to create a Stream in Java 8

  • First example to create a empty Stream 
package com.nourit.java8.Streams;

import java.util.stream.Stream;
/**
 * @author NOUR-IT.COM
 *
 */
public class CreateStream1 {

  public static void main(String[] args) {
    Stream<String> streamEmpty = Stream.empty();
    //An empty stream has no elements
    streamEmpty.forEach(System.out::println);
  }
}
  • Second example to create a Stream with Stream of Collection
package com.nourit.java8.Streams;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;

/**
 * @author NOUR-IT.COM
 *
 */
public class CreateStream2 {

  public static void main(String[] args) {
    Collection<String> collection = Arrays.asList("NOUR", "-", "IT", ".", "COM");
    Stream<String> streamOfCollection = collection.stream();
    streamOfCollection.forEach(System.out::print);
  }
}

Output:

NOUR-IT.COM
  • Third example to create a Stream with Stream of Array
package com.nourit.java8.Streams;

import java.util.Arrays;
import java.util.stream.Stream;

/**
 * @author NOUR-IT.COM
 *
 */
public class CreateStream3 {

  public static void main(String[] args) {
    // First Example
    Stream<String> streamOfArray1 = Stream.of("NOUR", "-", "IT", ".", "COM");
    streamOfArray1.forEach(System.out::print);
    System.out.println();

    // Second Example
    String[] items1 = new String[] { "NOUR", "-", "IT", ".", "COM" };
    Stream<String> streamOfArray2 = Arrays.stream(items1);
    streamOfArray2.forEach(System.out::print);
    System.out.println();

    // Third Example
    String[] items2 = new String[] { "NOUR", "-", "IT", ".", "COM" };
    Stream<String> streamOfArray3 = Stream.of(items2);
    streamOfArray3.forEach(System.out::print);
    System.out.println();
    
    // Fourth Example
    String[] items3 = new String[] { "NOUR", "-", "IT", ".", "COM" };
    Stream<String> streamOfArray4 = Arrays.stream(items3, 0, 5);
    streamOfArray4.forEach(System.out::print);
  }

}

Output:

NOUR-IT.COM
NOUR-IT.COM
NOUR-IT.COM
NOUR-IT.COM
  • Fourth example to create a Stream with Stream iterate
package com.nourit.java8.Streams;

import java.util.stream.IntStream;
import java.util.stream.Stream;
 /** * @author NOUR-IT.COM * */
public class CreateStream4 {

  public static void main(String[] args) {
    //Creates a list of all numbers from 0 to 9
    IntStream.iterate(0, i -> i + 1)
                .limit(10)
                .forEach(System.out::print);
    
    	System.out.println();
    	//Creates a list of all numbers from 10 to 28
        Stream<Integer> streamIterated = Stream.iterate(10, n -> n + 2).limit(10);
        streamIterated.forEach(System.out::print);

  }

}

Output:

0123456789
10121416182022242628
  • Sixth example to create a Stream with Builder
package com.nourit.java8.Streams;

import java.util.stream.Stream;

public class CreateStream5 {

  public static void main(String[] args) {
     System.out.println("---------------------------------------------");
     Stream.builder()
         .add("WWW")
         .add(".")
         .add("NOUR-IT")
         .add(".")
         .add("COM")
         .build().forEachOrdered(System.out::print);
     
     System.out.println();
     System.out.println("-------------------- OR -------------------------");
     
     Stream<Object> stream  =  Stream.<Object>builder()
             .add("WWW")
             .add(".")
             .add("NOUR-IT")
             .add(".")
             .add("COM")
             .build();
     stream.forEach(System.out::print);
     System.out.println();
     System.out.println("---------------------------------------------");
  }

}

Output:

---------------------------------------------
WWW.NOUR-IT.COM
-------------------- OR -------------------------
WWW.NOUR-IT.COM
---------------------------------------------
  • Seventh example to create a Stream with generate
package com.nourit.java8.Streams;

import java.util.stream.Stream;

public class CreateStreamGenerate {

  public static void main(String[] args) {
    //5 generate random numbers
    Stream.generate(() -> Double.valueOf(Math.random() * 50).intValue()).limit(5).forEach(System.out::println);
    System.out.println("-----------------------");
    //This example creates a sequence of five strings with the value - "WWW.NOUR-IT.COM".
    Stream<String> streamGenerated =
          Stream.generate(() -> "WWW.NOUR-IT.COM").limit(5);
    streamGenerated.forEach(System.out::println);
  }

}

Output:

18
0
27
23
45
-----------------------
WWW.NOUR-IT.COM
WWW.NOUR-IT.COM
WWW.NOUR-IT.COM
WWW.NOUR-IT.COM
WWW.NOUR-IT.COM

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

2 Gedanken zu „How to create a Stream in Java 8?

  • 2. September 2018 um 20:03
    Permalink

    Great examples of programming. very useful to understand the concept

    Antwort
  • Pingback: vioglichfu

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*