Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java 8 reduce by example

farenda 2016-10-22 0

Java 8 reduce is a great method to compute a value from stream of values – reduce collection to value. In this post we’ll explain how reduce in Java work.

reduce(T initial, BinaryOperator accumulator)

This version takes initial value, goes through all the values in a stream, and passes each of them, with result of previous computation, to the accumulator function to compute the result (maybe sounds complex, but it’s simple):

List<Integer> numbers = asList(1, 2, 3, 4, 5);
int sum = numbers.stream().reduce(0, (a, b) -> {
    System.out.printf("summing(%s, %s)%n", a, b);
    return Integer.sum(a, b);
});
System.out.println("Sum: " + sum);

In the output you can see that on the left is already accumulated value (initially 0) and on the right is subsequent value from the stream:

summing(0, 1)
summing(1, 2)
summing(3, 3)
summing(6, 4)
summing(10, 5)
Sum: 15

reduce(BinaryOperator accumulator) to Optional

This version returns Optional, because it doesn’t have initial value, and when the stream would be empty the reduction wouldn’t be able to return any value. Compare these two examples:

List<Integer> numbers = asList(1, 2, 3, 4, 5);
Optional<Integer> result = numbers.stream()
    .reduce(Integer::sum);
int sum = result.orElse(0);
System.out.println("Sum: " + sum);

The above code produces the following output:

Sum: 15

If the collection would be empty the value from orElse(0) would be used as in this example:

// empty list == empty stream!
List<Integer> numbers = emptyList();
Optional<Integer> result = numbers.stream()
    .reduce(Integer::sum);
int sum = result.orElse(0);
System.out.println("Sum: " + sum);

The code gives:

Sum: 0

reduce(T initial, BiFunction accumulator, BinaryOperator combiner)

This, three parameter, reduction is designed to be used with parallelStream, else it will behave as normal reduce with initial value and accumulator function. First, let’s create a simple combiner that will log values being combined:

static BinaryOperator<Integer> COMBINER = (a, b) -> {
    System.out.printf("combining(%s, %s)%n", a, b);
    return Integer.sum(a, b);
};

In the first example we’re going to use the combiner version, but on simple, non-parallel stream:

List<Integer> numbers = asList(1, 2, 3, 4, 5);
int sum = numbers.stream()
    // init, accumulator, combiner
    .reduce(0, Integer::sum, COMBINER);
System.out.println("Sum: " + sum);

As you can see, the program prints only the accumulated value, because the combiner haven’t been called:

Sum: 15

Only when we use the parallelStream the combiner comes into action:

List<Integer> numbers = asList(1, 2, 3, 4, 5);
int sum = numbers.parallelStream()
    // init, accumulator, combiner
    .reduce(0, Integer::sum, COMBINER);
System.out.println("Sum: " + sum);

Now the intermediate values, accumulated by accumulator function, are passed to the combiner for final accumulation:

combining(4, 5)
combining(3, 9)
combining(1, 2)
combining(3, 12)
Sum: 15
Share with the World!
Categories Java Tags java, java-util, java8
Previous: Maven use company repository
Next: Java String to int

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored