6

Similar questions have been asked, here and here, but given the advent of Java 8, and the generally outdated nature of these questions I'm wondering if now there'd be something at least kindred to it?

This is what I'm referring to.

2
  • Stream.reduce, perhaps? Also consider Stream.collect which offers better performance for many use cases. Jan 8, 2015 at 14:03
  • Whilst being an equivalent, Java's reduce is not as versatile as Python's. You cannot for example provide an initial value of a different type and reduce sequentially based on your current result and an element in the stream. Because you also need to provide a combiner bi-function that should provide a consistent result, e.g. a+b=b+a.
    – ahanin
    Apr 6, 2016 at 9:36

3 Answers 3

5

You can use a lambda and Stream.reduce, there is a page in the docs dedicated to reductions:

Integer totalAgeReduce = roster
   .stream()
   .map(Person::getAge)
   .reduce(
       0,
       (a, b) -> a + b);
4

This is the example used in the Python docs implemented with Java 8 streams:

List<Integer> numbers = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5 });
Optional<Integer> sum = numbers.stream().reduce((a, b) -> a + b);
System.out.println(sum.get());
2
  • @zehnpaard reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) from the page the OP linked to
    – Sylwester
    Jan 8, 2015 at 17:13
  • Ah sorry I misunderstood, I thought you meant that the Java code appeared in the Python docs somewhere, perhaps as part of comparisons of similar techniques in other languages.
    – zehnpaard
    Jan 9, 2015 at 5:21
3

The Stream.reduce Method

The Stream.reduce method is a general-purpose reduction operation. Consider the following pipeline, which calculates the sum of the male members' ages in the collection roster. It uses the Stream.sum reduction operation:

 Integer totalAge = roster
     .stream()
    .mapToInt(Person::getAge)
    .sum();

Compare this with the following pipeline, which uses the Stream.reduce operation to calculate the same value:

Integer totalAgeReduce = roster
   .stream()
   .map(Person::getAge)
   .reduce(
       0,
       (a, b) -> a + b);

The reduce operation in this example takes two arguments:

identity: The identity element is both the initial value of the reduction and the default result if there are no elements in the stream. In this example, the identity element is 0; this is the initial value of the sum of ages and the default value if no members exist in the collection roster.

accumulator: The accumulator function takes two parameters: a partial result of the reduction (in this example, the sum of all processed integers so far) and the next element of the stream (in this example, an integer). It returns a new partial result. In this example, the accumulator function is a lambda expression that adds two Integer values and returns an Integer value:

(a, b) -> a + b

The reduce operation always returns a new value. However, the accumulator function also returns a new value every time it processes an element of a stream. Suppose that you want to reduce the elements of a stream to a more complex object, such as a collection. This might hinder the performance of your application. If your reduce operation involves adding elements to a collection, then every time your accumulator function processes an element, it creates a new collection that includes the element, which is inefficient. It would be more efficient for you to update an existing collection instead. You can do this with the Stream.collect method, which the next section describes.

The official oracle tutorial describes how the Stream.reduce works. Please have a look, I believe it will answer your query.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.