Working with Stream Terminal Operations

  • google plus

In Brief...

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.

Take our Introduction to Java Training course for free.

See the Course Outline and Register

Instructions

  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.stream.*;
    import java.util.*;
    public class WorkingWithTerminalOperations {
    	private static List numbers=new ArrayList<>();
    	static {
    		numbers.add(50);
    		numbers.add(1);
    		numbers.add(204);
    		numbers.add(-27);
    		numbers.add(27);
    		numbers.add(-204);
    		numbers.add(9);
    		numbers.add(40);
    	}
    	public static void main (String args[]) {
    		System.out.println("The array list integers: ");
    		numbers.stream().forEach(System.out::println);
    		
    		Optional min=numbers.stream().min(Integer::compare);
    		System.out.println("Minimum value: " + min.get());
    		
    		Optional max=numbers.stream().max(Integer::compare);
    		System.out.println("Maximum value: " + max.get());
    		
    		Optional total=numbers.stream().reduce((i,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 WorkingWithTerminalOperations.java.
  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' experience in training, development, and consulting in a variety of technology areas including Java, C, C++, XML, JavaScript, AJAX, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen is a published writer in both technical and non-technical endeavors. Stephen received an undergraduate degree in Computer Science and Physics from Florida State University.

Discuss