Stream reduce() Method Example In Java

The reduce() method is a part of terminal operations in java stream. It performs operations to produce a single value as result by reducing, such as average(), sum(), min(), max(), and count() etc. The reduce() method applies the binary operator to each of the elements to reduce it to a single value. The return type of the reduce() method is type T or it can be an Optional.

  1. We can use Stream.reduce()
  2. Syntex -> Optional<Integer> max = intList.stream().reduce(( i, j ) -> i > j ? i : j);
  3. The return type of the reduce() method is type T or it can be an Optional.
  4. The reduce method is a part of terminal operations in java stream.
  5. It performs operations to produce a single value as result by reducing, such as average(), sum(), min(), max(), and count() etc.
  6. The reduce() method applies the binary operator to each element to reduce it to a single value.

Syntax:

 Stream.reduce() 

Java Reduce Method Example

//Importing required classes
import java.util.ArrayList;
import java.util.List;
import java.util.stream.*;

public class ReduceMethodExample {
  public static void main(String args[]){
    //Declaring List
    List<Integer> intList = new ArrayList<Integer>();
    intList.add( 6 );
    intList.add( 11 );
    intList.add( 14 );
    intList.add( 7 );
    intList.add( 3 );
    intList.add( 2 );
    intList.add( 16 );
    intList.add( 15 );
    intList.add( 19 );

    Optional<Integer> max = 
            intList.stream()
            .reduce(( i, j ) -> i > j ? i : j);

    System.out.println( max )
    System.out.println( max.get() )
}
}

Output:
    Optional[19]
    19

Java Reduce Method Sum Example

//Importing required classes
import java.util.ArrayList;
import java.util.List;
import java.util.stream.*;

public class ReduceMethodExample {
  public static void main(String args[]){
    //Declaring List
    List<Integer> intList = new ArrayList<Integer>();
    intList.add( 6 );
    intList.add( 3 );
    intList.add( 2 );
    intList.add( 4 );
    intList.add( 5 );

    //sum of all elements
    int sum = intList.stream().reduce(0, 
                ( i, j ) -> i + j);

    System.out.println( sum )
  }
}

Output:
    20

Java Reduce Method IntStream range Example

//Importing required classes
import java.util.ArrayList;
import java.util.List;
import java.util.stream.*;

public class ReduceMethodExample {
  public static void main( String args[] ){
    //Multiplication of all elements with in range
    //Range will be started from 2 to 5
    //Its similar to 2*3*4*5
    iint multiplicationValue = IntStream.range(2, 6)
        .reduce(( num1, num2 ) -> num1 * num2 )
        .orElse(-1);

   System.out.println( multiplicationValue )
  }
}

Output:
    120

Java Reduce Method String Concat Example

//Importing required classes
import java.util.ArrayList;
import java.util.List;
import java.util.stream.*;

public class ReduceMethodExample {
  public static void main(String args[]){
    List<String> strList = new ArrayList<String>();
    strList.add( "One" );
    strList.add( "Two" );
    strList.add( "Three" );
    strList.add( "Four" );
    strList.add( "Five" );

    Optional<String> concatString = strList.stream()
            .reduce(( str1, str2 )
                    -> str1 + "-" + str2);

    System.out.println( concatString );
    System.out.println( concatString.get() );
  }
}

Output:
    Optional[One-Two-Three-Four-Five]
    One-Two-Three-Four-Five