Streams in Java 8 are designed to process collections of data in a functional style. After creating or transforming a stream, you often need to display its elements for debugging, verification, or output purposes. Java provides several ways to print stream elements depending on the use case.
- Stream elements are printed using terminal operations.
- forEach() is the most commonly used method for displaying stream elements.
- Streams can only be traversed once, so they cannot be reused after printing.
Methods to Print Elements of a Stream
Java provides following multiple ways to print the elements of a stream.
1. Print Stream Elements Using forEach()
The forEach() method is a terminal operation that performs the specified action on every element of the stream. It is the most common and recommended way to print stream elements.
Syntax:
stream.forEach(System.out::println);
The forEach() method prints each stream element using the method reference System.out::println, providing a cleaner and more concise alternative to a lambda expression.
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// Get the stream
Stream<String> stream = Stream.of("Geeks", "For",
"Geeks", "A",
"Computer", "Portal");
// Print the stream
stream.forEach(System.out::println);
}
}
Output
Geeks For Geeks A Computer Portal
Example: Using lambda expression with forEach() method
Stream is created using Stream.of(), and the forEach() method traverses each element. A lambda expression is then used to print every element of the stream individually.
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// Get the stream
Stream<String> stream = Stream.of("Geeks", "For",
"Geeks", "A",
"Computer", "Portal");
// Print the stream
stream.forEach(s -> System.out.println(s));
}
}
Output
Geeks For Geeks A Computer Portal
2. Print Stream Elements Using forEachOrdered()
The forEachOrdered() method prints elements while preserving the encounter order of the stream. It is particularly useful when working with parallel streams.
Syntax:
stream.forEachOrdered(System.out::println);
Example: Using Method Reference with forEachOrdered() Method
The following example demonstrates how forEachOrdered() prints stream elements using the method reference System.out::println while maintaining their encounter order.
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
Stream<String> stream
= Stream.of("Geeks", "For", "Geeks",
"A", "Computer", "Portal")
.parallel();
stream.forEachOrdered(System.out::println);
}
}
Output
Geeks For Geeks A Computer Portal
Example: Using Lambda Expression with forEachOrdered() Method
The following example demonstrates how forEachOrdered() uses a lambda expression to print stream elements while preserving their encounter order.
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
Stream<String> stream
= Stream.of("Geeks", "For", "Geeks",
"A", "Computer", "Portal")
.parallel();
stream.forEachOrdered(s -> System.out.println(s));
}
}
Output
Geeks For Geeks A Computer Portal
3. Print Stream Elements Using collect()
Instead of printing elements directly, you can collect them into a collection and then print the collection.
Syntax:
System.out.println (stream.collect(Collectors.toList()) );
Example: Using collect(Collectors.toList())
The following example demonstrates how stream elements can be collected into a List and then printed.
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
Stream<String> stream
= Stream.of("Geeks", "For", "Geeks");
List<String> list = stream.collect(Collectors.toList());
System.out.println(list);
}
}
Output
[Geeks, For, Geeks]
Example: Using collect(Collectors.toSet())
The following example demonstrates how stream elements can be collected into a Set, which automatically removes duplicate elements.
import java.util.*;
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
Stream<String> stream
= Stream.of("Geeks", "For", "Geeks");
Set<String> set = stream.collect(Collectors.toSet());
System.out.println(set);
}
}
Output
[Geeks, For]
Note: The order of elements in a HashSet is not guaranteed, so the output order may vary.
4. Print Stream Elements Using peek()
The peek() method is mainly intended for debugging. It allows you to inspect elements while they pass through the stream pipeline.
Syntax:
stream.peek(System.out::println).terminalOperation();
Example: Using peek() with a Terminal Operation
The following example demonstrates how peek() prints each stream element before the terminal operation count() is executed.
import java.util.stream.*;
class GFG {
public static void main(String[] args) {
Stream.of("Geeks", "For", "GeeksForGeeks", "A", "Computer", "Portal")
.filter(s -> s.startsWith("G"))
.peek(s -> System.out.println("Filtered: " + s))
.map(String::toUpperCase)
.peek(s -> System.out.println("Uppercase: " + s))
.forEach(s -> {});
}
}
Output
Filtered: Geeks Uppercase: GEEKS Filtered: GeeksForGeeks Uppercase: GEEKSFORGEEKS