Java 8 Streams - collect vs reduce

前端 未结 7 1724
天涯浪人
天涯浪人 2020-11-28 01:17

When would you use collect() vs reduce()? Does anyone have good, concrete examples of when it\'s definitely better to go one way or the other?

相关标签:
7条回答
  • 2020-11-28 02:14

    According to the docs

    The reducing() collectors are most useful when used in a multi-level reduction, downstream of groupingBy or partitioningBy. To perform a simple reduction on a stream, use Stream.reduce(BinaryOperator) instead.

    So basically you'd use reducing() only when forced within a collect. Here's another example:

     For example, given a stream of Person, to calculate the longest last name 
     of residents in each city:
    
        Comparator<String> byLength = Comparator.comparing(String::length);
        Map<String, String> longestLastNameByCity
            = personList.stream().collect(groupingBy(Person::getCity,
                reducing("", Person::getLastName, BinaryOperator.maxBy(byLength))));
    

    According to this tutorial reduce is sometimes less efficient

    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...

    So the identity is "re-used" in a reduce scenario, so slightly more efficient to go with .reduce if possible.

    0 讨论(0)
提交回复
热议问题