Home » Java programming language

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.
Submitted by Preeti Jain, on July 30, 2019

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() method of Stream
  3. peek() method 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.

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() method 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.

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

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

Example:

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 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.

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:

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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.