This is a cache of http://dbflute.seasar.org/ja/manual/topic/programming/java/java8/mapandflat.html. It is a snapshot of the page at 2024-11-01T00:40:42.238+0000.
Java - map() and flatMap() | DBFlute

map() and flatMap()

概要

コピーして自身のテキストエディターで見たほうが見やすいです。

e.g. map() and flatMap() view @Java

# ========================================================================================
#                                                                               List map()
#                                                                               ==========
List<Member&gt; memberList = memberBhv.selectList(...);
List<Integer&gt; memberIdList = memberList.stream()
                                       .map(mb -&gt; mb.getMemberId()) // returns Integer
                                       .collect(Collectors.toList());

 +- List<Member&gt; ---+                  |- List<Integer&gt; -----+
 |                  |                  |                     |
 |  {1, "sea"}      | ---- map() ----&gt; |       1             |
 |  {2, "land"}     | mb.getMemberId() |       2             |
 |  {3, "piary"}    |                  |       3             |
 |                  |                  |                     |
 +------------------+                  +---------------------+

 empty list:
 +- List<Member&gt; ---+                  |- List<Integer&gt; -----+
 |                  |                  |                     |
 |  *empty          | ---- map() ----&gt; |     *empty          |
 |                  | mb.getMemberId() |                     |
 |                  |                  |                     |
 +------------------+                  +---------------------+

# ========================================================================================
#                                                                           List flatMap()
#                                                                           ==============
List<Member&gt; memberList = memberBhv.selectList(...);
memberBhv.loadPurchase(memberList, ...);
List<Purchase&gt; purchaseList
        = memberList.stream()
                    .flatMap(mb -&gt; mb.getPurchaseList().stream()) // returns Stream
                    .collect(Collectors.toList());

 +- List<Member&gt; ---------------+                      +- List<Purchase&gt; ----+
 |                              |                      |                     |
 |  {1, "sea"}                  |                      |   {101, 1, ...}     |
 |    |-{101, 1, ...}           |                      |   {102, 1, ...}     |
 |    |-{102, 1, ...}           | ---- flatMap() ----&gt; |   {103, 2, ...}     |
 |  {2, "land"}                 | mb.getPurchaseList() |   {104, 2, ...}     |
 |    |-{103, 2, ...}           |   .stream()          |   {105, 3, ...}     |
 |    |-{104, 2, ...}           |                      |                     |
 |  {3, "piary"}                |                      +---------------------+
 |    |-{105, 3, ...}           |
 |                              |
 +------------------------------+

 without flat:
 +- List<Member&gt; ---------------+                      +- List<List<Purchase&gt;&gt; --+
 |                              |                      |                         |
 |  {1, "sea"}                  |                      |  +-----------------+    |
 |    |-{101, 1, ...}           |                      |  | {101, 1, ...}   |    |
 |    |-{102, 1, ...}           |                      |  | {102, 1, ...}   |    |
 |  {2, "land"}                 | ------ map() ------&gt; |  +-----------------+    |
 |    |-{103, 2, ...}           | mb.getPurchaseList() |  | {103, 2, ...}   |    |
 |    |-{104, 2, ...}           |                      |  | {104, 2, ...}   |    |
 |  {3, "piary"}                |                      |  +-----------------+    |
 |    |-{105, 3, ...}           |                      |  | {105, 3, ...}   |    |
 |                              |                      |  +-----------------+    |
 +------------------------------+                      +-------------------------+

 empty list:
 +- List<Member&gt; ---------------+                      +- List<Purchase&gt; ----+
 |                              |                      |                     |
 |    *empty                    | ---- flatMap() ----&gt; |   *empty            |
 |                              | mb.getPurchaseList() |                     |
 |                              |   .stream()          |                     |
 +------------------------------+                      +---------------------+

# ========================================================================================
#                                                                           Optional map()
#                                                                           ==============
Optional<Member&gt; optMember = memberBhv.selectEntity(...);
Optional<Integer&gt; optMemberId = optMember.map(mb -&gt; mb.getMemberId()); // returns Integer

 +- Optional<Member&gt; --+                   +- Optional<Integer&gt; ---+
 |                     |                   |                       |
 | {1, "sea"}          | ---- map() -----&gt; |  (1)                  |
 |                     | mb.getMemberId()  |                       |
 +---------------------+                   +-----------------------+

 +- Optional<Member&gt; --+                   +- Optional<Integer&gt; ---+
 |                     |                   |                       |
 | *empty              | ---- map() -----&gt; | *empty                |
 |                     | mb.getMemberId()  |                       |
 +---------------------+                   +-----------------------+

 return null:
 +- Optional<Member&gt; --+                   +- Optional<Integer&gt; -+
 |                     |                   |                     |
 | {null, "sea"}       | ---- map() -----&gt; | *empty              |
 |                     | mb.getMemberId()  |                     |
 +---------------------+                   +---------------------+

 detail:
 +- Optional<Member&gt; --+                                         +- Optional<Integer&gt; ---+
 |                     |                                         |                       |
 |                     | ------------ map() -------------------&gt; |                       |
 |                     |                                         |                       |
 | {1, "sea"}          | if present:                             |                       |
 |  or                 |   mb.getMemberId() =&gt; (1) ----(wrap)---/`\---&gt; (1)              |
 | {null, "sea"}       |                    =&gt; null              |                       |
 |  or                 |                          \------+       |       or              |
 | *empty              | else                            |       |                       |
 |                     |   *empty-------(cast only)------+-----&gt;&gt;|      *empty           |
 |                     |                                         |                       |
 +---------------------+                                         +-----------------------+

# ========================================================================================
#                                                                       Optional flatMap()
#                                                                       ==================
Optional<Member&gt; optMember = memberBhv.selectEntity(...);
Optional<MemberWithdrawal&gt; optWdl = optMember.flatMap(mb -&gt; mb.getMemberWithdrawalAsOne()) // returns Optional

 +- Optional<Member&gt; ---------------------+                               +- Optional<MemberWithdrawal&gt; ----+
 |                                        |                               |                                 |
 |  mb:{1, "sea"}                         | -------- flatMap() ---------&gt; |   wdl:{1, 2016/7/20, ...}       |
 |   |                                    | mb.getMemberWithdrawalAsOne() |                                 |
 |   |                                    |                               +---------------------------------+
 |   | +- Optional<MemberWithdrawal&gt; -+   |                                   ^
 |   +-| wdl:{1, 2016/7/20, ...}      |--/`\--&gt;---&gt;---&gt;---&gt;---&gt;---&gt;---&gt;---&gt;---^
 |     +------------------------------+   |
 +----------------------------------------+

 +- Optional<Member&gt; ---------------------+                               +- Optional<MemberWithdrawal&gt; ----+
 |                                        |                               |                                 |
 |  mb:{1, "sea"}                         | -------- flatMap() ---------&gt; |   *empty                        |
 |   |                                    | mb.getMemberWithdrawalAsOne() |                                 |
 |   |                                    |                               +---------------------------------+
 |   | +- Optional<MemberWithdrawal&gt; -+   |                                   ^
 |   +-| *empty                       |--/`\--&gt;---&gt;---&gt;---&gt;---&gt;---&gt;---&gt;---&gt;---^
 |     +------------------------------+   |
 +----------------------------------------+

 +- Optional<Member&gt; ---------------------+                               +- Optional<MemberWithdrawal&gt; ----+
 |                                        |                               |                                 |
 |  *empty                                | -------- flatMap() ---------&gt; |   *empty                        |
 |                                        | mb.getMemberWithdrawalAsOne() |                                 |
 +----------------------------------------+                               +---------------------------------+

 without flat:
 +- Optional<Member&gt; --------------------+                               +- Optional<Optional<MemberWithdrawal&gt;&gt; -+
 |                                       |                               |                                        |
 |  mb:{1, "sea"}                        | ----------- map() ----------&gt; |   +- Optional<MemberWithdrawal&gt; -+     |
 |   |                                   | mb.getMemberWithdrawalAsOne() |   | {101, 1, ...}                |     |
 |   |                                   |                               |   +------------------------------+     |
 |   | +- Optional<MemberWithdrawal&gt; -+  |                               |       ^                                |
 |   +-| wdl:{1, 2016/7/20, ...}      |-/`\--&gt;---&gt;---&gt;---&gt;---&gt;---&gt;---&gt;--/`\--&gt;---^                                |
 |     +------------------------------+  |                               |                                        |
 +---------------------------------------+                               +----------------------------------------+

 detail:
 +- Optional<Member&gt; -------------+                                             +- Optional<MemberWithdrawal&gt; -+
 |                                |                                             |                              |
 |                                | -------------- flatMap() -----------------&gt; |                              |
 |                                |                                             |                              |
 | mb:{1, "sea"} - wdl:{101, ...} | if present:                                 |                              |
 |                                |   mb.getMemberWithdrawalAsOne()             |                              |
 |  or                            |     =&gt; Optional({101, ...})                 |                              |
 |                                |            \--(non wrap)---(null check)---&gt;&gt;|   {101, ...}                 |
 | mb:{2, "land"} - *empty        |     =&gt; *empty                               |                              |
 |  or                            |            \------------------+             |    or                        |
 | *empty                         | else                          |             |                              |
 |                                |   *empty -------(cast only)---+-----------&gt;&gt;|   *empty                     |
 |                                |                                             |                              |
 +--------------------------------+                                             +------------------------------+