Last Updated : 11 Jul, 2025
Introduced in Java 8,
the Stream APIis used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. The features of Java stream are –
There are 3 ways to print the elements of a Stream in Java:
Below are the three ways to print the Stream in detail:
void forEach(Consumer<? super T> action) Where, Consumer is a functional interface and T is the type of stream elements.Below is how to print elements of Stream using forEach() method: Program 1: Java
// Java code to print the elements of Stream
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 PortalProgram 2: Using Short hand lambda expression Java
// Java code to print the elements of Stream
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 PortalProgram 3: This approach consumes the stream and makes it unavailable for future use. Hence the below code will throw an error since the stream is already consumed. Java
// Java code to print the elements of Stream
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));
// Since the stream has been already consumed
// this will throw exception
try {
// Print the stream
stream.forEach(s -> System.out.println(s));
}
catch (Exception e) {
System.out.println("\nException: " + e);
}
}
}
Output:
Geeks For Geeks A Computer Portal Exception: java.lang.IllegalStateException: stream has already been operated upon or closed
System.out.println(stream.collect(Collectors.toList()));Program 1: Java
// Java code to print the elements of Stream
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
System.out.println(stream.collect(Collectors.toList()));
}
}
Output:
[Geeks, For, Geeks, A, Computer, Portal]Program 2: This approach also consumes the stream and makes it unavailable for future use. Hence the below code will throw an error since the stream is already consumed. Java
// Java code to print the elements of Stream
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
System.out.println(stream.collect(Collectors.toList()));
// Since the stream has been already consumed
// this will throw exception
try {
// Print the stream
System.out.println(stream.collect(Collectors.toList()));
}
catch (Exception e) {
System.out.println("\nException: " + e);
}
}
}
Output:
[Geeks, For, Geeks, A, Computer, Portal] Exception: java.lang.IllegalStateException: stream has already been operated upon or closed
Stream<T> peek(Consumer<? super T> action) Where, Stream is an interface and T is the type of stream elements. action is a non-interfering action to perform on the elements as they are consumed from the stream and the function returns the new stream.Program 1: Java
// Java code to print the elements of Stream
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 using peek()
// by providing a terminal operation count()
stream.peek(s -> System.out.println(s)).count();
}
}
Output:
Geeks For Geeks A Computer PortalProgram 2: This approach do not consumes the stream. Hence the below code will not throw any error. Java
// Java code to print the elements of Stream
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// Get the stream
Stream<String> stream = Stream.of("Geeks", "For",
"GeeksForGeeks", "A",
"Computer", "Portal");
// Since the stream is not being consumed
// this will not throw any exception
// Print the stream
stream.filter(s -> s.startsWith("G"))
.peek(s -> System.out.println("Filtered value: " + s))
.map(String::toUpperCase)
.peek(s -> System.out.println("Uppercase value :" + s))
.count();
}
}
Output:
Filtered value: Geeks Uppercase value :GEEKS Filtered value: GeeksForGeeks Uppercase value :GEEKSFORGEEKS
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4