How to print elements of a Stream in Java 8?

Printing elements of a Stream in Java 8: Here, we are going to learn about the different ways to print the elements of a Stream in Java 8. By Preeti Jain Last updated : March 23, 2024

Printing elements of a Stream

In Java, there are three different ways to print the elements of a Stream in java 8. The name of these 3 different ways is given below:

  1. forEach() method of Stream
  2. println() with collect() methods of Stream
  3. peek() method of Stream

We will see all the three ways of printing the elements of a stream in java 8 one by one...

1) forEach() method of Stream

  • This method is available in java.util.stream package.
  • This is not the static method so this method will be called with the object.
  • The return type of this method is void so it does not return anything.
  • This method acts as each element of the stream.

Syntax

The syntax of this method is given below,

void forEach(Consumer <? super T > consumer);

Here, Consumer is an interface and T is the element type.

Example: Non lambda expression

import java.util.stream.*;

public class PrintStreamElementByForeachMethod {
    public static void main(String[] args) {
        // Here of() method of Stream interface is used to get the stream
        Stream stm = Stream.of("Java", "is", "a", "programming", "language");

        // we are printing the stream by using forEach() method
        stm.forEach(stm1 -> System.out.println(stm1));
    }
}

Output

E:\Programs>javac PrintStreamElementByForeachMethod.java
E:\Programs>java PrintStreamElementByForeachMethod
Java
is
a
programming
language

Example: Short hand lambda expression

import java.util.stream.*;

public class PrintStreamElementByForeachMethod {
    public static void main(String[] args) {
        // Here of() method of Stream interface is used to get the stream
        Stream stm = Stream.of("Java", "is", "a", "programming", "language");

        // we are printing the stream by using forEach() method
        stm.forEach(System.out::println);
    }
}

Output

E:\Programs>javac PrintStreamElementByForeachMethod.java

E:\Programs>java PrintStreamElementByForeachMethod
Java
is
a
programming
language

2) println() with collect() methods of Stream

  • This method is available in java.util.stream package.
  • This method is not static so it will be accessible with objects of Stream interface.
  • This method collects stream elements as a Collector object and then print the elements by using println() method.

Syntax

The syntax of println() with collect() method,

System.out.println(Stream_object.collect(Collectors.toList()));

Example to print elements of a Stream using println() with collect() methods

import java.util.stream.*;

public class PrintStreamElementByForeachMethod {
    public static void main(String[] args) {
        // Here of() method of Stream interface is used to get the stream
        Stream stm = Stream.of("Java", "is", "a", "programming", "language");

        // we are printing the stream by using forEach() method
        stm.forEach(System.out::println);
    }
}

Output

E:\Programs>javac PrintStreamElementByForeachMethod.java

E:\Programs>java PrintStreamElementByForeachMethod
[Java, is, a, programming, language]

3) peek() method of Stream

  • This method is available in java.util.stream package.
  • This method is not static so this method will be called with the object of Stream.

Syntax

The syntax of this method is given below:

    Stream peek(Consumer <? super T> consumer);

This method returns a Stream and it consists of all the elements of the Current stream and it performs the given operation or action on each element.

In this method, if a stream is already consumed then the same stream we want to consume again then, in that case, we will not get any error or exception and it is valid.

Example to print elements of a Stream using peek() method

import java.util.stream.*;

public class PrintStreamElementByPeekMethod {
    public static void main(String[] args) {
        // Here of() method of Stream interface is used to get the stream
        Stream stm = Stream.of("Java", "is", "a", "programming", "language");

        //  we are printing the stream by using peek() method 
        //  and it provides the facility count() method to terminate 
        stm.peek(stm1 -> System.out.println(stm1)).count();
    }
}

Output

E:\Programs>javac PrintStreamElementByPeekMethod.java

E:\Programs>java PrintStreamElementByPeekMethod
Java
is
a
programming
language


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.