Working with Stream Terminal Operations

A terminal operation in Java is a method applied to a stream as the final step. Additional stream operations are not permitted because a terminal operation never produces a stream object. A common example of a terminal operation is the forEach method that often is used to print the stream object's elements. Another useful example is the reduce method that produces a single result (e.g., a sum) from a stream. Other good examples include min and max. The following four steps show how to work with stream terminal operations.

  1. Open your text editor and create the Java program that will create the stream and apply terminal operations. Type in the following Java statements:
    import java.util.*;
    public class WorkingWithTerminalOperations {
    	private static List numbers=new ArrayList<>();
    	static {
    	public static void main (String args[]) {
    		System.out.println("The array list integers: ");;
    		System.out.println("Minimum value: " + min.get());
    		System.out.println("Maximum value: " + max.get());
    		Optional,j)-> i+j);
    		System.out.println("Sum: " + total.get());
    The stream will be created from the ArrayList that is defined on line 4. Note that a static block is used to add elements to the ArrayList. A static block is executed one time when the program is loaded into memory. The program will display the list of integers and then apply several terminal operations. The first terminal operation is forEach, and this operation will print each element of the stream. Next, the min operation is called. This method returns the minimum value stored in the stream as an Optional. Optional is a new class in Java 8 that provides a get method to return the value of the variable if the value is non-null. The next operation is max. This method returns the maximum value stored in the stream. Finally, the reduce method is called. The parameter passed to this method is a lambda expression. In this case, the expression adds one element to the next element, accumulating a total that is returned as an Optional. I have highlighted the terminal operations in the image below:
    Java Source for Terminal Operations
  2. Save your file as
  3. Open a command prompt and navigate to the directory containing your new Java program. Then type in the command to compile the source and hit Enter.
    Compile Program with Terminal Operations
  4. You are ready to test your Java program. Type in the command to run the Java runtime launcher and hit Enter. The output displays the collection of integers, followed by the minimum value, the maximum value, and the sum of the integers.
    Run Program with Terminal Operations
Author: Stephen Withrow

Stephen has over 30 years of experience in training, development, and consulting in a variety of technology areas including Python, Java, C, C++, XML, JavaScript, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen has a degree in Computer Science and Physics from Florida State University.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.