When you need to combine all stream elements into a single result (like summing prices, finding the longest string, or concatenating values), the reduce operation folds elements together using a binary operator.

Reduce with Identity

The reduce operation applies an accumulator function across elements, collapsing the stream into one result. When you provide an identity value, reduce always returns a result (never empty).

nums
Identity.java
Replay: real traced execution (multi-file project)
import java.util.*;

public class Identity {
    public static void main(String[] args) {
        List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5);

        int sum = nums.stream().reduce(0, (a, b) -> a + b);
        System.out.println("sum = " + sum);

        int product = nums.stream().reduce(1, (a, b) -> a * b);
        System.out.println("product = " + product);

        int max = nums.stream().reduce(Integer.MIN_VALUE, Math::max);
        System.out.println("max = " + max);

        int emptySum = Collections.<Integer>emptyList().stream().reduce(0, Integer::sum);
        System.out.println("emptySum = " + emptySum);

    }
}
import java.util.*;

public class Identity {
    public static void main(String[] args) {
        List<Integer> nums = Arrays.asList(2, 4, 6);

        int sum = nums.stream().reduce(0, (a, b) -> a + b);
        System.out.println("sum = " + sum);

        int product = nums.stream().reduce(1, (a, b) -> a * b);
        System.out.println("product = " + product);

        int max = nums.stream().reduce(Integer.MIN_VALUE, Math::max);
        System.out.println("max = " + max);

        int emptySum = Collections.<Integer>emptyList().stream().reduce(0, Integer::sum);
        System.out.println("emptySum = " + emptySum);

    }
}
import java.util.*;

public class Identity {
    public static void main(String[] args) {
        List<Integer> nums = Arrays.asList(10);

        int sum = nums.stream().reduce(0, (a, b) -> a + b);
        System.out.println("sum = " + sum);

        int product = nums.stream().reduce(1, (a, b) -> a * b);
        System.out.println("product = " + product);

        int max = nums.stream().reduce(Integer.MIN_VALUE, Math::max);
        System.out.println("max = " + max);

        int emptySum = Collections.<Integer>emptyList().stream().reduce(0, Integer::sum);
        System.out.println("emptySum = " + emptySum);

    }
}
  1. nums ← [1, 2, 3, 4, 5], sum ← 15, product ← 120, max ← 5, emptySum ← 0

    3public class Identity {4    public static void main(String[] args) {5        List<Integer> nums→ [1, 2, 3, 4, 5] = Arrays.asList(1, 2, 3, 4, 5); //@nums=Arrays.asList(1, 2, 3, 4, 5), Arrays.asList(2, 4, 6), Arrays.asList(10)67        int sum→ 15 = nums.stream().reduce(0, (a, b) -> a + b);8        System.out.println("sum = " + sum15);910        int product→ 120 = nums.stream().reduce(1, (a, b) -> a * b);11        System.out.println("product = " + product120);1213        int max→ 5 = nums.stream().reduce(Integer.MIN_VALUE, Math::max);14        System.out.println("max = " + max5);1516        int emptySum→ 0 = Collections.<Integer>emptyList().stream().reduce(0, Integer::sum);17        System.out.println("emptySum = " + emptySum0);
    outputsum = 15
    product = 120
    max = 5
    emptySum = 0
  1. nums ← [2, 4, 6], sum ← 12, product ← 48, max ← 6, emptySum ← 0

    3public class Identity {4    public static void main(String[] args) {5        List<Integer> nums→ [2, 4, 6] = Arrays.asList(2, 4, 6);67        int sum→ 12 = nums.stream().reduce(0, (a, b) -> a + b);8        System.out.println("sum = " + sum12);910        int product→ 48 = nums.stream().reduce(1, (a, b) -> a * b);11        System.out.println("product = " + product48);1213        int max→ 6 = nums.stream().reduce(Integer.MIN_VALUE, Math::max);14        System.out.println("max = " + max6);1516        int emptySum→ 0 = Collections.<Integer>emptyList().stream().reduce(0, Integer::sum);17        System.out.println("emptySum = " + emptySum0);
    outputsum = 12
    product = 48
    max = 6
    emptySum = 0
  1. nums ← [10], sum ← 10, product ← 10, max ← 10, emptySum ← 0

    3public class Identity {4    public static void main(String[] args) {5        List<Integer> nums→ [10] = Arrays.asList(10);67        int sum→ 10 = nums.stream().reduce(0, (a, b) -> a + b);8        System.out.println("sum = " + sum10);910        int product→ 10 = nums.stream().reduce(1, (a, b) -> a * b);11        System.out.println("product = " + product10);1213        int max→ 10 = nums.stream().reduce(Integer.MIN_VALUE, Math::max);14        System.out.println("max = " + max10);1516        int emptySum→ 0 = Collections.<Integer>emptyList().stream().reduce(0, Integer::sum);17        System.out.println("emptySum = " + emptySum0);
    outputsum = 10
    product = 10
    max = 10
    emptySum = 0
reduce A terminal operation that combines all elements into a single value by repeatedly applying a binary operator.
identity A starting value for reduce that, when combined with any element, yields that element, such as 0 for addition or 1 for multiplication.

Reduce without Identity

Without an identity, reduce returns an Optional since an empty stream has no result.

nums
OptionalReduce.java
Replay: real traced execution (multi-file project)
import java.util.*;

public class OptionalReduce {
    public static void main(String[] args) {
        List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5);

        Optional<Integer> sum = nums.stream().reduce((a, b) -> a + b);
        System.out.println("sum = " + sum.orElse(0));

        Optional<Integer> max = nums.stream().reduce((a, b) -> a > b ? a : b);
        System.out.println("max = " + max.orElse(-1));

        Optional<Integer> emptySum = Collections.<Integer>emptyList().stream().reduce(Integer::sum);
        System.out.println("emptySum = " + emptySum.orElse(999));

    }
}
import java.util.*;

public class OptionalReduce {
    public static void main(String[] args) {
        List<Integer> nums = Arrays.asList(9, 2);

        Optional<Integer> sum = nums.stream().reduce((a, b) -> a + b);
        System.out.println("sum = " + sum.orElse(0));

        Optional<Integer> max = nums.stream().reduce((a, b) -> a > b ? a : b);
        System.out.println("max = " + max.orElse(-1));

        Optional<Integer> emptySum = Collections.<Integer>emptyList().stream().reduce(Integer::sum);
        System.out.println("emptySum = " + emptySum.orElse(999));

    }
}
import java.util.*;

public class OptionalReduce {
    public static void main(String[] args) {
        List<Integer> nums = Collections.emptyList();

        Optional<Integer> sum = nums.stream().reduce((a, b) -> a + b);
        System.out.println("sum = " + sum.orElse(0));

        Optional<Integer> max = nums.stream().reduce((a, b) -> a > b ? a : b);
        System.out.println("max = " + max.orElse(-1));

        Optional<Integer> emptySum = Collections.<Integer>emptyList().stream().reduce(Integer::sum);
        System.out.println("emptySum = " + emptySum.orElse(999));

    }
}
  1. nums ← [1, 2, 3, 4, 5], sum ← Optional[15], max ← Optional[5]

    3public class OptionalReduce {4    public static void main(String[] args) {5        List<Integer> nums→ [1, 2, 3, 4, 5] = Arrays.asList(1, 2, 3, 4, 5); //@nums=Arrays.asList(1, 2, 3, 4, 5), Arrays.asList(9, 2), Collections.emptyList()67        Optional<Integer> sum→ Optional[15] = nums.stream().reduce((a, b) -> a + b);8        System.out.println("sum = " + sum.orElse(0));910        Optional<Integer> max→ Optional[5] = nums.stream().reduce((a, b) -> a > b ? a : b);11        System.out.println("max = " + max.orElse(-1));1213        Optional<Integer> emptySum→ Optional.empty = Collections.<Integer>emptyList().stream().reduce(Integer::sum);14        System.out.println("emptySum = " + emptySum.orElse(999));
    outputsum = 15
    max = 5
    emptySum = 999
  1. nums ← [9, 2], sum ← Optional[11], max ← Optional[9], emptySum ← Optional.empty

    3public class OptionalReduce {4    public static void main(String[] args) {5        List<Integer> nums→ [9, 2] = Arrays.asList(9, 2);67        Optional<Integer> sum→ Optional[11] = nums.stream().reduce((a, b) -> a + b);8        System.out.println("sum = " + sum.orElse(0));910        Optional<Integer> max→ Optional[9] = nums.stream().reduce((a, b) -> a > b ? a : b);11        System.out.println("max = " + max.orElse(-1));1213        Optional<Integer> emptySum→ Optional.empty = Collections.<Integer>emptyList().stream().reduce(Integer::sum);14        System.out.println("emptySum = " + emptySum.orElse(999));
    outputsum = 11
    max = 9
    emptySum = 999
  1. nums ← [], sum ← Optional.empty, max ← Optional.empty, emptySum ← Optional.empty

    3public class OptionalReduce {4    public static void main(String[] args) {5        List<Integer> nums→ [] = Collections.emptyList();67        Optional<Integer> sum→ Optional.empty = nums.stream().reduce((a, b) -> a + b);8        System.out.println("sum = " + sum.orElse(0));910        Optional<Integer> max→ Optional.empty = nums.stream().reduce((a, b) -> a > b ? a : b);11        System.out.println("max = " + max.orElse(-1));1213        Optional<Integer> emptySum→ Optional.empty = Collections.<Integer>emptyList().stream().reduce(Integer::sum);14        System.out.println("emptySum = " + emptySum.orElse(999));
    outputsum = 0
    max = -1
    emptySum = 999

Custom Reductions

You can reduce to any type by providing an accumulator and combiner for parallel safety.

separator
Custom.java
Replay: real traced execution (multi-file project)
import java.util.*;

public class Custom {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("hello", "world", "stream");
        String separator = ", ";

        String concat = words.stream().reduce("", (a, b) -> a + b);
        System.out.println("concat = '" + concat + "'");

        String joined = words.stream().reduce("", (a, b) -> a.isEmpty() ? b : a + separator + b);
        System.out.println("joined = '" + joined + "'");

        List<Integer> nums = Arrays.asList(10, 20, 30);
        int count = nums.stream().reduce(0, (acc, n) -> acc + 1, Integer::sum);
        System.out.println("count = " + count);

        int totalLength = words.stream().reduce(0, (acc, s) -> acc + s.length(), Integer::sum);
        System.out.println("totalLength = " + totalLength);

    }
}
import java.util.*;

public class Custom {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("hello", "world", "stream");
        String separator = " | ";

        String concat = words.stream().reduce("", (a, b) -> a + b);
        System.out.println("concat = '" + concat + "'");

        String joined = words.stream().reduce("", (a, b) -> a.isEmpty() ? b : a + separator + b);
        System.out.println("joined = '" + joined + "'");

        List<Integer> nums = Arrays.asList(10, 20, 30);
        int count = nums.stream().reduce(0, (acc, n) -> acc + 1, Integer::sum);
        System.out.println("count = " + count);

        int totalLength = words.stream().reduce(0, (acc, s) -> acc + s.length(), Integer::sum);
        System.out.println("totalLength = " + totalLength);

    }
}
  1. words ← [hello, world, stream], separator ← , , concat ← helloworldstream

    3public class Custom {4    public static void main(String[] args) {5        List<String> words→ [hello, world, stream] = Arrays.asList("hello", "world", "stream");6        String separator→ ,  = ", "; //@separator=", ", " | "78        String concat→ helloworldstream = words.stream().reduce("", (a, b) -> a + b);9        System.out.println("concat = '" + concathelloworldstream + "'");1011        String joined→ hello, world, stream = words.stream().reduce("", (a, b) -> a.isEmpty() ? b : a + separator + b);12        System.out.println("joined = '" + joinedhello, world, stream + "'");1314        List<Integer> nums→ [10, 20, 30] = Arrays.asList(10, 20, 30);15        int count→ 3 = nums.stream().reduce(0, (acc, n) -> acc + 1, Integer::sum);16        System.out.println("count = " + count3);1718        int totalLength→ 16 = words.stream().reduce(0, (acc, s) -> acc + s.length(), Integer::sum);19        System.out.println("totalLength = " + totalLength16);
    outputconcat = 'helloworldstream'
    joined = 'hello, world, stream'
    count = 3
    totalLength = 16
  1. words ← [hello, world, stream], separator ← | , concat ← helloworldstream

    3public class Custom {4    public static void main(String[] args) {5        List<String> words→ [hello, world, stream] = Arrays.asList("hello", "world", "stream");6        String separator→  |  = " | ";78        String concat→ helloworldstream = words.stream().reduce("", (a, b) -> a + b);9        System.out.println("concat = '" + concathelloworldstream + "'");1011        String joined→ hello | world | stream = words.stream().reduce("", (a, b) -> a.isEmpty() ? b : a + separator + b);12        System.out.println("joined = '" + joinedhello | world | stream + "'");1314        List<Integer> nums→ [10, 20, 30] = Arrays.asList(10, 20, 30);15        int count→ 3 = nums.stream().reduce(0, (acc, n) -> acc + 1, Integer::sum);16        System.out.println("count = " + count3);1718        int totalLength→ 16 = words.stream().reduce(0, (acc, s) -> acc + s.length(), Integer::sum);19        System.out.println("totalLength = " + totalLength16);
    outputconcat = 'helloworldstream'
    joined = 'hello | world | stream'
    count = 3
    totalLength = 16

Specialized Reductions

Streams provide shortcuts for common reductions: sum(), min(), max(), count().

nums
Specialized.java
Replay: real traced execution (multi-file project)
import java.util.*;
import java.util.stream.*;

public class Specialized {
    public static void main(String[] args) {
        List<Integer> nums = Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6);

        int sumViaReduce = nums.stream().reduce(0, Integer::sum);
        int sumViaMethod = nums.stream().mapToInt(Integer::intValue).sum();
        System.out.println("sumViaReduce = " + sumViaReduce);
        System.out.println("sumViaMethod = " + sumViaMethod);

        long countViaReduce = nums.stream().reduce(0L, (acc, n) -> acc + 1, Long::sum);
        long countViaMethod = nums.stream().count();
        System.out.println("countViaReduce = " + countViaReduce);
        System.out.println("countViaMethod = " + countViaMethod);

        Optional<Integer> maxViaReduce = nums.stream().reduce(Math::max);
        Optional<Integer> maxViaMethod = nums.stream().max(Integer::compareTo);
        System.out.println("maxViaReduce = " + maxViaReduce.orElse(-1));
        System.out.println("maxViaMethod = " + maxViaMethod.orElse(-1));

    }
}
import java.util.*;
import java.util.stream.*;

public class Specialized {
    public static void main(String[] args) {
        List<Integer> nums = Arrays.asList(10, 20, 30);

        int sumViaReduce = nums.stream().reduce(0, Integer::sum);
        int sumViaMethod = nums.stream().mapToInt(Integer::intValue).sum();
        System.out.println("sumViaReduce = " + sumViaReduce);
        System.out.println("sumViaMethod = " + sumViaMethod);

        long countViaReduce = nums.stream().reduce(0L, (acc, n) -> acc + 1, Long::sum);
        long countViaMethod = nums.stream().count();
        System.out.println("countViaReduce = " + countViaReduce);
        System.out.println("countViaMethod = " + countViaMethod);

        Optional<Integer> maxViaReduce = nums.stream().reduce(Math::max);
        Optional<Integer> maxViaMethod = nums.stream().max(Integer::compareTo);
        System.out.println("maxViaReduce = " + maxViaReduce.orElse(-1));
        System.out.println("maxViaMethod = " + maxViaMethod.orElse(-1));

    }
}
import java.util.*;
import java.util.stream.*;

public class Specialized {
    public static void main(String[] args) {
        List<Integer> nums = Arrays.asList(-2, 0, 8);

        int sumViaReduce = nums.stream().reduce(0, Integer::sum);
        int sumViaMethod = nums.stream().mapToInt(Integer::intValue).sum();
        System.out.println("sumViaReduce = " + sumViaReduce);
        System.out.println("sumViaMethod = " + sumViaMethod);

        long countViaReduce = nums.stream().reduce(0L, (acc, n) -> acc + 1, Long::sum);
        long countViaMethod = nums.stream().count();
        System.out.println("countViaReduce = " + countViaReduce);
        System.out.println("countViaMethod = " + countViaMethod);

        Optional<Integer> maxViaReduce = nums.stream().reduce(Math::max);
        Optional<Integer> maxViaMethod = nums.stream().max(Integer::compareTo);
        System.out.println("maxViaReduce = " + maxViaReduce.orElse(-1));
        System.out.println("maxViaMethod = " + maxViaMethod.orElse(-1));

    }
}
  1. nums ← [3, 1, 4, 1, 5, 9, 2, 6], sumViaReduce ← 31, sumViaMethod ← 31

    4public class Specialized {5    public static void main(String[] args) {6        List<Integer> nums→ [3, 1, 4, 1, 5, 9, 2, 6] = Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6); //@nums=Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6), Arrays.asList(10, 20, 30), Arrays.asList(-2, 0, 8)78        int sumViaReduce→ 31 = nums.stream().reduce(0, Integer::sum);9        int sumViaMethod→ 31 = nums.stream().mapToInt(Integer::intValue).sum();10        System.out.println("sumViaReduce = " + sumViaReduce31);11        System.out.println("sumViaMethod = " + sumViaMethod31);1213        long countViaReduce→ 8 = nums.stream().reduce(0L, (acc, n) -> acc + 1, Long::sum);14        long countViaMethod→ 8 = nums.stream().count();15        System.out.println("countViaReduce = " + countViaReduce8);16        System.out.println("countViaMethod = " + countViaMethod8);1718        Optional<Integer> maxViaReduce→ Optional[9] = nums.stream().reduce(Math::max);19        Optional<Integer> maxViaMethod→ Optional[9] = nums.stream().max(Integer::compareTo);20        System.out.println("maxViaReduce = " + maxViaReduce.orElse(-1));21        System.out.println("maxViaMethod = " + maxViaMethod.orElse(-1));
    outputsumViaReduce = 31
    sumViaMethod = 31
    countViaReduce = 8
    countViaMethod = 8
    maxViaReduce = 9
    maxViaMethod = 9
  1. nums ← [10, 20, 30], sumViaReduce ← 60, sumViaMethod ← 60, countViaReduce ← 3

    4public class Specialized {5    public static void main(String[] args) {6        List<Integer> nums→ [10, 20, 30] = Arrays.asList(10, 20, 30);78        int sumViaReduce→ 60 = nums.stream().reduce(0, Integer::sum);9        int sumViaMethod→ 60 = nums.stream().mapToInt(Integer::intValue).sum();10        System.out.println("sumViaReduce = " + sumViaReduce60);11        System.out.println("sumViaMethod = " + sumViaMethod60);1213        long countViaReduce→ 3 = nums.stream().reduce(0L, (acc, n) -> acc + 1, Long::sum);14        long countViaMethod→ 3 = nums.stream().count();15        System.out.println("countViaReduce = " + countViaReduce3);16        System.out.println("countViaMethod = " + countViaMethod3);1718        Optional<Integer> maxViaReduce→ Optional[30] = nums.stream().reduce(Math::max);19        Optional<Integer> maxViaMethod→ Optional[30] = nums.stream().max(Integer::compareTo);20        System.out.println("maxViaReduce = " + maxViaReduce.orElse(-1));21        System.out.println("maxViaMethod = " + maxViaMethod.orElse(-1));
    outputsumViaReduce = 60
    sumViaMethod = 60
    countViaReduce = 3
    countViaMethod = 3
    maxViaReduce = 30
    maxViaMethod = 30
  1. nums ← [-2, 0, 8], sumViaReduce ← 6, sumViaMethod ← 6, countViaReduce ← 3

    4public class Specialized {5    public static void main(String[] args) {6        List<Integer> nums→ [-2, 0, 8] = Arrays.asList(-2, 0, 8);78        int sumViaReduce→ 6 = nums.stream().reduce(0, Integer::sum);9        int sumViaMethod→ 6 = nums.stream().mapToInt(Integer::intValue).sum();10        System.out.println("sumViaReduce = " + sumViaReduce6);11        System.out.println("sumViaMethod = " + sumViaMethod6);1213        long countViaReduce→ 3 = nums.stream().reduce(0L, (acc, n) -> acc + 1, Long::sum);14        long countViaMethod→ 3 = nums.stream().count();15        System.out.println("countViaReduce = " + countViaReduce3);16        System.out.println("countViaMethod = " + countViaMethod3);1718        Optional<Integer> maxViaReduce→ Optional[8] = nums.stream().reduce(Math::max);19        Optional<Integer> maxViaMethod→ Optional[8] = nums.stream().max(Integer::compareTo);20        System.out.println("maxViaReduce = " + maxViaReduce.orElse(-1));21        System.out.println("maxViaMethod = " + maxViaMethod.orElse(-1));
    outputsumViaReduce = 6
    sumViaMethod = 6
    countViaReduce = 3
    countViaMethod = 3
    maxViaReduce = 8
    maxViaMethod = 8

Reduce vs Collect

Use reduce for immutable aggregations (sum, max, concatenate). Use collect for mutable reductions (building collections, StringBuilder).

minLength
Pipeline.java
Replay: real traced execution (multi-file project)
import java.util.*;

public class Pipeline {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");
        int minLength = 6;

        int totalLength = words.stream()
                .filter(s -> s.length() >= minLength)
                .map(String::length)
                .reduce(0, Integer::sum);
        System.out.println("totalLength = " + totalLength);

        List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6);
        int evenProduct = nums.stream()
                .filter(n -> n % 2 == 0)
                .reduce(1, (a, b) -> a * b);
        System.out.println("evenProduct = " + evenProduct);

        Optional<String> longest = words.stream()
                .reduce((a, b) -> a.length() > b.length() ? a : b);
        System.out.println("longest = " + longest.orElse(""));

    }
}
import java.util.*;

public class Pipeline {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");
        int minLength = 5;

        int totalLength = words.stream()
                .filter(s -> s.length() >= minLength)
                .map(String::length)
                .reduce(0, Integer::sum);
        System.out.println("totalLength = " + totalLength);

        List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6);
        int evenProduct = nums.stream()
                .filter(n -> n % 2 == 0)
                .reduce(1, (a, b) -> a * b);
        System.out.println("evenProduct = " + evenProduct);

        Optional<String> longest = words.stream()
                .reduce((a, b) -> a.length() > b.length() ? a : b);
        System.out.println("longest = " + longest.orElse(""));

    }
}
import java.util.*;

public class Pipeline {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");
        int minLength = 8;

        int totalLength = words.stream()
                .filter(s -> s.length() >= minLength)
                .map(String::length)
                .reduce(0, Integer::sum);
        System.out.println("totalLength = " + totalLength);

        List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6);
        int evenProduct = nums.stream()
                .filter(n -> n % 2 == 0)
                .reduce(1, (a, b) -> a * b);
        System.out.println("evenProduct = " + evenProduct);

        Optional<String> longest = words.stream()
                .reduce((a, b) -> a.length() > b.length() ? a : b);
        System.out.println("longest = " + longest.orElse(""));

    }
}
  1. words ← [apple, banana, cherry, date, elderberry], minLength ← 6

    3public class Pipeline {4    public static void main(String[] args) {5        List<String> words→ [apple, banana, cherry, date, elderberry] = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");6        int minLength→ 6 = 6; //@minLength=6, 5, 878        int totalLength→ 22 = words.stream()9                .filter(s -> s.length() >= minLength)10                .map(String::length)11                .reduce(0, Integer::sum);12        System.out.println("totalLength = " + totalLength22);1314        List<Integer> nums→ [1, 2, 3, 4, 5, 6] = Arrays.asList(1, 2, 3, 4, 5, 6);15        int evenProduct→ 48 = nums.stream()16                .filter(n -> n % 2 == 0)17                .reduce(1, (a, b) -> a * b);18        System.out.println("evenProduct = " + evenProduct48);1920        Optional<String> longest→ Optional[elderberry] = words.stream()21                .reduce((a, b) -> a.length() > b.length() ? a : b);22        System.out.println("longest = " + longest.orElse(""));
    outputtotalLength = 22
    evenProduct = 48
    longest = elderberry
  1. words ← [apple, banana, cherry, date, elderberry], minLength ← 5

    3public class Pipeline {4    public static void main(String[] args) {5        List<String> words→ [apple, banana, cherry, date, elderberry] = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");6        int minLength→ 5 = 5;78        int totalLength→ 27 = words.stream()9                .filter(s -> s.length() >= minLength)10                .map(String::length)11                .reduce(0, Integer::sum);12        System.out.println("totalLength = " + totalLength27);1314        List<Integer> nums→ [1, 2, 3, 4, 5, 6] = Arrays.asList(1, 2, 3, 4, 5, 6);15        int evenProduct→ 48 = nums.stream()16                .filter(n -> n % 2 == 0)17                .reduce(1, (a, b) -> a * b);18        System.out.println("evenProduct = " + evenProduct48);1920        Optional<String> longest→ Optional[elderberry] = words.stream()21                .reduce((a, b) -> a.length() > b.length() ? a : b);22        System.out.println("longest = " + longest.orElse(""));
    outputtotalLength = 27
    evenProduct = 48
    longest = elderberry
  1. words ← [apple, banana, cherry, date, elderberry], minLength ← 8

    3public class Pipeline {4    public static void main(String[] args) {5        List<String> words→ [apple, banana, cherry, date, elderberry] = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");6        int minLength→ 8 = 8;78        int totalLength→ 10 = words.stream()9                .filter(s -> s.length() >= minLength)10                .map(String::length)11                .reduce(0, Integer::sum);12        System.out.println("totalLength = " + totalLength10);1314        List<Integer> nums→ [1, 2, 3, 4, 5, 6] = Arrays.asList(1, 2, 3, 4, 5, 6);15        int evenProduct→ 48 = nums.stream()16                .filter(n -> n % 2 == 0)17                .reduce(1, (a, b) -> a * b);18        System.out.println("evenProduct = " + evenProduct48);1920        Optional<String> longest→ Optional[elderberry] = words.stream()21                .reduce((a, b) -> a.length() > b.length() ? a : b);22        System.out.println("longest = " + longest.orElse(""));
    outputtotalLength = 10
    evenProduct = 48
    longest = elderberry

Exercise: Practical.java

Reduce a list of orders to calculate total revenue and find the largest order