[Java] Use Collectors.collectingAndThen

What is Collectors.collectingAndThen?

A Collector that returns the processed value after the Collect.

How to use

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

Collectors.Form using collectingAndThen


return /* Stream */
  ./*Stream processing such as map*/
  .collect(
      Collectors.collectingAndThen(
          Collectors.toList(),
          mappedList -> hogeFunc(mappedList) //Processing using mappedList
      )
  );

What are you happy about?

Temporary variables decrease

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.

Readability is higher than direct delivery

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

Summary

Let's use Collectors.collectingAndThen.

Recommended Posts

[Java] Use Collectors.collectingAndThen
Use OpenCV in Java
Use PreparedStatement in Java
[Java] How to use Map
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
Java
[Java] Use Big Decimal properly ~ 2018 ~
[java] Reasons to use static
How to use Java variables
Use Redis Stream in Java
CICS-Run Java applications-(5) Use JCICSX
Java
Use SpatiaLite with Java / JDBC
[Java] How to use Optional ①
How to use Java HttpClient (Get)
Use java with MSYS and Cygwin
Use Microsoft Graph with standard Java
How to use Java HttpClient (Post)
[Java] How to use join method
Let's use Twilio in Java! (Introduction)
Use Azure Bing SpellCheck with Java
Use JDBC with Java and Scala.
[Java] Do not use "+" in append!
Use composite keys in Java Map.
[Processing × Java] How to use variables
Use Java 11 with Google Cloud Functions
[Java] How to use LinkedHashMap class
java (use class type for field)
Use Chrome Headless from Selenium / Java
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
[Processing × Java] How to use arrays
How to use Java lambda expressions
Do you use Stream in Java?
[Java] How to use Math class
How to use Java enum type
Java learning (0)
Use before_action! !!
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Multilingual Locale in Java How to use Locale
Use OpenCV_Contrib (ArUco) in Java! (Part 2-Programming)
Java methods
Java method
java (constructor)
Java array
[Java] How to use the File class