How to print elements of a Stream in Java 8 Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Introduced in Java 8, the Stream API is 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 – A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels. Streams don’t change the original data structure, they only provide the result as per the pipelined methods. Each intermediate operation is lazily executed and returns a stream as a result, hence various intermediate operations can be pipelined. Terminal operations mark the end of the stream and return the result. There are 3 ways to print the elements of a Stream in Java: forEach() println() with collect() peek() Below are the three ways to print the Stream in detail: Stream forEach(Consumer action): This method performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : 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 Portal Program 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 Portal Program 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 Using println() with collect(): This method collects the elements of the stream as a collector instance, for example as List. Hence the printing of List can be done easily using println() method. Syntax: 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 peek(Consumer action): This method returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. This is an intermediate operation i.e, it creates a new stream that, when traversed, contains the elements of the initial stream that match the given predicate. Syntax : 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 Portal Program 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 Comment More infoAdvertise with us Next Article Convert a Set to Stream in Java R RishabhPrabhu Follow Improve Article Tags : Java Practice Tags : Java Similar Reads How to Get ArrayList from Stream in Java 8? Given a Stream, the task is to convert this Stream into ArrayList in Java 8. Examples:Input: Stream: [1, 2, 3, 4, 5]Output: ArrayList: [1, 2, 3, 4, 5]Input: Stream: ['G', 'e', 'e', 'k', 's']Output: ArrayList: ['G', 'e', 'e', 'k', 's']1. Using Collectors.toList() method: Approach:Get the Stream to be 2 min read PrintStream print(char[]) method in Java with Examples The print(char[]) method ofPrintStream Class in Java is used to print the specified character array on the stream. This character array is taken as a parameter. Syntax: public void print(char[] charArray) Parameters: This method accepts a mandatory parameter charArray which is the character array to 2 min read PrintStream print(char[]) method in Java with Examples The print(char[]) method ofPrintStream Class in Java is used to print the specified character array on the stream. This character array is taken as a parameter. Syntax: public void print(char[] charArray) Parameters: This method accepts a mandatory parameter charArray which is the character array to 2 min read Convert a Set to Stream in Java Set interface extends Collection interface and Collection has stream() method that returns a sequential stream of the collection. Below given are some examples to understand the implementation in a better way. Example 1 : Converting Integer HashSet to Stream of Integers. Java // Java code for conver 2 min read Stream mapToInt() in Java with examples Stream mapToInt(ToIntFunction mapper) returns an IntStream consisting of the results of applying the given function to the elements of this stream. Stream mapToInt(ToIntFunction mapper) is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream in 2 min read PrintStream print(int) method in Java with Examples The print(int) method of PrintStream Class in Java is used to print the specified int value on the stream. This int value is taken as a parameter. Syntax: public void print(int intValue) Parameters: This method accepts a mandatory parameter intValue which is the int value to be written on the stream 2 min read Like