{"id":181,"date":"2018-09-28T11:36:53","date_gmt":"2018-09-28T03:36:53","guid":{"rendered":"https:\/\/www.espandy.com\/?p=181"},"modified":"2018-09-28T11:36:53","modified_gmt":"2018-09-28T03:36:53","slug":"stream-collectors-groupingby-%e4%be%8b%e5%ad%90","status":"publish","type":"post","link":"https:\/\/www.espandy.com\/?p=181","title":{"rendered":"Stream Collectors groupingBy \u4f8b\u5b50"},"content":{"rendered":"<\/p>\n<p><span style=\"display: inline-block;\"><span style=\"color: rgba(0, 0, 0, 0.87); font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; font-size: 2rem; font-weight: 700;\">Stream Collectors groupingBy \u4f8b\u5b50&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size: 26px;\"><b>\u8f6c\u8f7d\uff1a<\/b><\/span><a href=\"https:\/\/my.oschina.net\/LiuLangEr\/blog\/1632378\">https:\/\/my.oschina.net\/LiuLangEr\/blog\/1632378<\/a><\/p>\n<p><a href=\"https:\/\/my.oschina.net\/LiuLangEr\">&nbsp;<\/a><a href=\"https:\/\/my.oschina.net\/LiuLangEr\">\u952e\u76d8\u4e0a\u8df3\u821e<\/a><span style=\"display: inline-block; table-layout: fixed; line-height: 1.14286em; color: rgba(0, 0, 0, 0.87); font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;\">&nbsp;<\/span><span style=\"display: inline-block; table-layout: fixed; line-height: 1.14286em; color: rgba(0, 0, 0, 0.87); font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;\">\u53d1\u5e03\u4e8e 03\/09 16:06<\/span><span style=\"color: rgba(0, 0, 0, 0.87); font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u5411\u60a8\u5c55\u793a\u5982\u4f55\u4f7f\u7528java 8 &nbsp;Stream&nbsp;<\/span><span style=\"font-size: 13px; line-height: 28px; color: rgb(61, 70, 77); font-family: monospace, monospace; border-radius: 3px; box-sizing: inherit; padding: 2px;\">Collectors<\/span><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">&nbsp;\u5bf9\u5217\u8868\u5206\u7ec4\uff0c\u8ba1\u6570\uff0c\u6c42\u548c\u548c\u6392\u5e8f\u3002<\/span><\/p>\n<h2><span style=\"font-size: 22px; line-height: 1.28571em; color: rgb(61, 70, 77); font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; font-weight: 500;\">1. Group By, Count and Sort<\/span><\/h2>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">1.1 Group by a&nbsp;<\/span><span style=\"font-size: 13px; line-height: 28px; color: rgb(61, 70, 77); font-family: monospace, monospace; border-radius: 3px; box-sizing: inherit; padding: 2px;\">List<\/span><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">&nbsp;and display the total count of it.\uff08\u6309\u5217\u8868\u5206\u7ec4\uff0c\u5e76\u663e\u793a\u5176\u603b\u6570\uff09<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">Java8Example1.java<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">package com.mkyong.java8;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.Arrays;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.List;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.Map;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.function.<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Function<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.stream.Collectors;<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">public<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">class<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(239, 239, 143); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Java8Example1<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">{<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">public<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">static<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">void<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">main(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">[] args) {<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(127, 159, 127); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">\/\/3 apple, 2 banana, others 1<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">List&lt;<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&gt; items =<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Arrays.asList(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;apple&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;apple&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;banana&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;apple&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;orange&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;banana&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;papaya&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">);<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Map<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&lt;<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">, Long&gt; result =<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">items.stream().collect(<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Collectors.groupingBy(<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Function<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">.identity(), Collectors.counting()<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">);<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">System.out.println(result);<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">output<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">{<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;papaya=1, orange=1, banana=2, apple=3<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">1.2 Add sorting.\uff08\u589e\u52a0\u6392\u5e8f\u5b9e\u73b0\uff09<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">Java8Example2.java<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">package com.mkyong.java8;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.Arrays;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.LinkedHashMap;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.List;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.Map;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.function.<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Function<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.stream.Collectors;<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">public<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">class<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(239, 239, 143); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Java8Example2<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">{<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">public<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">static<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">void<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">main(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">[] args) {<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(127, 159, 127); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">\/\/3 apple, 2 banana, others 1<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">List&lt;<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&gt; items =<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Arrays.asList(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;apple&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;apple&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;banana&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;apple&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;orange&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;banana&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;papaya&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">);<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Map<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&lt;<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">, Long&gt; result =<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">items.stream().collect(<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Collectors.groupingBy(<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Function<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">.identity(), Collectors.counting()<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">);<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Map<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&lt;<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">, Long&gt; finalMap =<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">LinkedHashMap&lt;&gt;();<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(127, 159, 127); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">\/\/Sort a map and add to finalMap<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">result.entrySet().stream()<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">.sorted(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Map<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">.Entry.&lt;<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">, Long&gt;comparingByValue()<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">.reversed()).forEachOrdered(e -&gt; finalMap.put(e.getKey(), e.getValue()));<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">System.out.println(finalMap);<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">output<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">{<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;apple=3, banana=2, papaya=1, orange=1<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<h2><span style=\"font-size: 22px; line-height: 1.28571em; color: rgb(61, 70, 77); font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; font-weight: 500;\">2. List Objects<\/span><\/h2>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">Examples to \u2018group by\u2019 a list of user defined Objects.\uff08\u901a\u8fc7\u201c\u7528\u6237\u5b9a\u4e49\u7684\u5bf9\u8c61\u201d\u5217\u8868\u8fdb\u884c\u5206\u7ec4\u7684\u793a\u4f8b\u3002\uff09<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">2.1 A Pojo.<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">Item.java<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">package<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">com.mkyong.java8;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.math.BigDecimal;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">public<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">class<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(239, 239, 143); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">{<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">private<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String name;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">private<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">int<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">qty;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">private<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal price;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(127, 159, 127); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">\/\/constructors, getter\/setters<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">2.2 Group by the name + Count or Sum the Qty. \uff08name + Count\u5206\u7ec4\u6216\u8005 \u5bf9 Qty\u6c42\u548c\u5206\u7ec4\uff09<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">Java8Examples3.java<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">package com.mkyong.java8;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.math.BigDecimal;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.Arrays;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.List;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.Map;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.stream.Collectors;<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">public<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">class<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(239, 239, 143); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Java8Examples3<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">{<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">public<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">static<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">void<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">main(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">[] args) {<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(127, 159, 127); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">\/\/3 apple, 2 banana, others 1<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">List&lt;Item&gt; items = Arrays.asList(<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;apple&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">10<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;9.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;banana&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">20<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;19.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;orang&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">10<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;29.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;watermelon&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">10<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;29.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;papaya&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">20<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;9.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;apple&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">10<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;9.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;banana&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">10<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;19.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;apple&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">20<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;9.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">))<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">);<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Map<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&lt;<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">, Long&gt; counting = items.stream().collect(<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Collectors.groupingBy(Item::getName, Collectors.counting()));<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">System.out.println(counting);<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Map<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&lt;<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">, Integer&gt; sum = items.stream().collect(<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">System.out.println(sum);<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">output<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">\/\/Group by + Count<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">{<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;papaya=1, banana=2, apple=3, orang=1, watermelon=1<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">\/\/Group by + Sum qty<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">{<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;papaya=20, banana=30, apple=40, orang=10, watermelon=10<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">2.2 &nbsp;Price \u5206\u7ec4 \u2013&nbsp;<\/span><span style=\"font-size: 13px; line-height: 28px; color: rgb(61, 70, 77); font-family: monospace, monospace; border-radius: 3px; box-sizing: inherit; padding: 2px;\">Collectors.groupingBy<\/span><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">&nbsp;and&nbsp;<\/span><span style=\"font-size: 13px; line-height: 28px; color: rgb(61, 70, 77); font-family: monospace, monospace; border-radius: 3px; box-sizing: inherit; padding: 2px;\">Collectors.mapping<\/span><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">\u4f8b\u5b50.<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">Java8Examples4.java<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">package com.mkyong.java8;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.math.BigDecimal;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.Arrays;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.List;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.Map;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.Set;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">import<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">java.util.stream.Collectors;<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">public<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">class<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(239, 239, 143); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Java8Examples4<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">{<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">public<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">static<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">void<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">main(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">[] args) {<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(127, 159, 127); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">\/\/3 apple, 2 banana, others 1<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">List&lt;Item&gt; items = Arrays.asList(<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;apple&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">10<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;9.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;banana&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">20<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;19.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;orang&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">10<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;29.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;watermelon&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">10<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;29.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;papaya&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">20<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;9.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;apple&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">10<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;9.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;banana&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">10<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;19.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)),<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Item(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;apple&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(140, 208, 211); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">20<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(227, 206, 171); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">new<\/span> <span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">BigDecimal(<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&quot;9.99&quot;<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">))<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">);<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(127, 159, 127); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">\/\/group by price<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Map<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&lt;BigDecimal, List&lt;Item&gt;&gt; groupByPriceMap =<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;items.stream().collect(Collectors.groupingBy(Item::getPrice));<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">System.out.println(groupByPriceMap);<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(127, 159, 127); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">\/\/ group by price, uses &apos;mapping&apos; to convert List&lt;Item&gt; to Set&lt;String&gt;<\/span><\/p>\n<p><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Map<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&lt;BigDecimal,<\/span> <span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Set<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&lt;<\/span><span style=\"box-sizing: inherit; font-size: 13px; line-height: 1.4; color: rgb(204, 147, 147); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">String<\/span><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&gt;&gt; result =<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">items.stream().collect(<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Collectors.groupingBy(Item::getPrice,<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">Collectors.mapping(Item::getName, Collectors.toSet())<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">)<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">);<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">System.out.println(result);<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 16px; line-height: 28px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif;\">output<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">{<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;19.99=[<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item{name=&apos;banana&apos;, qty=20, price=19.99},<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item{name=&apos;banana&apos;, qty=10, price=19.99}<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;29.99=[<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item{name=&apos;orang&apos;, qty=10, price=29.99},<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item{name=&apos;watermelon&apos;, qty=10, price=29.99}<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;9.99=[<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item{name=&apos;apple&apos;, qty=10, price=9.99},<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item{name=&apos;papaya&apos;, qty=20, price=9.99},<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item{name=&apos;apple&apos;, qty=10, price=9.99},<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item{name=&apos;apple&apos;, qty=20, price=9.99}<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">\/\/group by + mapping to Set<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">{<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;19.99=[banana],<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;29.99=[orang, watermelon],<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">&nbsp;&nbsp;&nbsp;&nbsp;9.99=[papaya, apple]<\/span><\/p>\n<p><span style=\"font-size: 13px; line-height: 1.4; color: rgb(220, 220, 220); font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;\">}<\/span><\/p>\n<p><span style=\"font-size: 12pt;\"><\/span><\/p>\n<p><span style=\"font-size: 1rem; line-height: 1.4285em; color: rgba(0, 0, 0, 0.87); font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif;\">\u00a9 \u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Stream Collectors groupingBy \u4f8b\u5b50&nbsp;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[18],"class_list":["post-181","post","type-post","status-publish","format-standard","hentry","category-life","tag-blog"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.espandy.com\/index.php?rest_route=\/wp\/v2\/posts\/181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.espandy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.espandy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.espandy.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.espandy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=181"}],"version-history":[{"count":0,"href":"https:\/\/www.espandy.com\/index.php?rest_route=\/wp\/v2\/posts\/181\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.espandy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.espandy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.espandy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}