Java Stream reduce operation

Introduction

The following program demonstrates the reduce():

// Demonstrate the reduce() method. 

import java.util.ArrayList;
import java.util.Optional;

public class Main {

  public static void main(String[] args) {

    // Create a list of Integer values.
    ArrayList<Integer> myList = new ArrayList<>();

    myList.add(7);//from  ww  w  .  jav a  2 s .com
    myList.add(18);
    myList.add(10);
    myList.add(24);
    myList.add(17);
    myList.add(5);

    // Two ways to obtain the integer product of the elements
    // in myList by use of reduce().
    Optional<Integer> productObj = myList.stream().reduce((a, b) -> a * b);
    if (productObj.isPresent())
      System.out.println("Product as Optional: " + productObj.get());

    int product = myList.stream().reduce(1, (a, b) -> a * b);
    System.out.println("Product as int: " + product);
  }
}



PreviousNext

Related