Collector that returns the processed value after the
Suppose you have the following situation:
Temporarily store in list → call process
List<Integer> mappedList = /* Stream */ ./*Stream processing such as map*/ .collect(Collectors.toList()); return hogeFunc(mappedList);
Such processing can be written as follows by using
Collectors.Form using collectingAndThen
return /* Stream */ ./*Stream processing such as map*/ .collect( Collectors.collectingAndThen( Collectors.toList(), mappedList -> hogeFunc(mappedList) //Processing using mappedList ) );
Since it is not necessary to store the Collect results in temporary variables one by one, the number of temporary variables can be reduced. Putting a temporary variable leads to poor readability and code quality, so I'm happy to reduce it.
The code example given at the beginning can also be written as:
When written in the form of passing arguments directly to the function
return hogeFunc( /* Stream */ ./*Stream processing such as map*/ .collect(Collectors.toList()) );
However, this is [^ hosoku], which is less readable, especially when functions are nested. In addition, this has the disadvantage that it cannot be read by hooking it with the debugger.
[^ hosoku]: For this scene, if you have a pipeline operator, you don't even need to use