{"id":610,"date":"2019-08-18T11:08:38","date_gmt":"2019-08-18T03:08:38","guid":{"rendered":"http:\/\/prayerlaputa.com\/?p=610"},"modified":"2019-08-18T11:08:38","modified_gmt":"2019-08-18T03:08:38","slug":"java8-%e5%bf%ab%e9%80%9f%e5%ae%9e%e7%8e%b0list%e8%bd%acmap-%e3%80%81%e5%88%86%e7%bb%84%e3%80%81%e8%bf%87%e6%bb%a4%e7%ad%89%e6%93%8d%e4%bd%9c%e3%80%90%e8%bd%ac%e8%bd%bd%e3%80%91","status":"publish","type":"post","link":"http:\/\/prayerlaputa.com\/?p=610","title":{"rendered":"java8 \u5feb\u901f\u5b9e\u73b0List\u8f6cmap \u3001\u5206\u7ec4\u3001\u8fc7\u6ee4\u7b49\u64cd\u4f5c\u3010\u8f6c\u8f7d\u3011"},"content":{"rendered":"<p>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3a\u535a\u4e3b\u539f\u521b\u6587\u7ae0\uff0c\u9075\u5faa CC 4.0 by-sa \u7248\u6743\u534f\u8bae\uff0c\u8f6c\u8f7d\u8bf7\u9644\u4e0a\u539f\u6587\u51fa\u5904\u94fe\u63a5\u548c\u672c\u58f0\u660e\u3002<br \/>\n\u672c\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/lu930124\/article\/details\/77595585<\/p>\n<p>\u5229\u7528java8\u65b0\u7279\u6027\uff0c\u53ef\u4ee5\u7528\u7b80\u6d01\u9ad8\u6548\u7684\u4ee3\u7801\u6765\u5b9e\u73b0\u4e00\u4e9b\u6570\u636e\u5904\u7406\u3002<\/p>\n<p>\u5b9a\u4e491\u4e2aApple\u5bf9\u8c61\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\r\npublic class Apple {\r\nprivate Integer id;\r\nprivate String name;\r\nprivate BigDecimal money;\r\nprivate Integer num;\r\npublic Apple(Integer id, String name, BigDecimal money, Integer num) {\r\nthis.id = id;\r\nthis.name = name;\r\nthis.money = money;\r\nthis.num = num;\r\n}\r\n}\r\n\r\n<\/pre>\n<p><!--more--><br \/>\n\u6dfb\u52a0\u4e00\u4e9b\u6d4b\u8bd5\u6570\u636e\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nList&lt;Apple&gt; appleList = new ArrayList&lt;&gt;();\/\/\u5b58\u653eapple\u5bf9\u8c61\u96c6\u5408\r\n\r\nApple apple1 = new Apple(1,&quot;\u82f9\u679c1&quot;,new BigDecimal(&quot;3.25&quot;),10);\r\nApple apple12 = new Apple(1,&quot;\u82f9\u679c2&quot;,new BigDecimal(&quot;1.35&quot;),20);\r\nApple apple2 = new Apple(2,&quot;\u9999\u8549&quot;,new BigDecimal(&quot;2.89&quot;),30);\r\nApple apple3 = new Apple(3,&quot;\u8354\u679d&quot;,new BigDecimal(&quot;9.99&quot;),40);\r\n\r\nappleList.add(apple1);\r\nappleList.add(apple12);\r\nappleList.add(apple2);\r\nappleList.add(apple3);\r\n\r\n<\/pre>\n<p>1\u3001\u5206\u7ec4<br \/>\nList\u91cc\u9762\u7684\u5bf9\u8c61\u5143\u7d20\uff0c\u4ee5\u67d0\u4e2a\u5c5e\u6027\u6765\u5206\u7ec4\uff0c\u4f8b\u5982\uff0c\u4ee5id\u5206\u7ec4\uff0c\u5c06id\u76f8\u540c\u7684\u653e\u5728\u4e00\u8d77\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\n\/\/List \u4ee5ID\u5206\u7ec4 Map&lt;Integer,List&lt;Apple&gt;&gt;\r\nMap&lt;Integer, List&lt;Apple&gt;&gt; groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));\r\n\r\nSystem.err.println(&quot;groupBy:&quot;+groupBy);\r\n\r\n<\/pre>\n<p>{1=[Apple{id=1, name=&#8217;\u82f9\u679c1&#8242;, money=3.25, num=10}, Apple{id=1, name=&#8217;\u82f9\u679c2&#8242;, money=1.35, num=20}], 2=[Apple{id=2, name=&#8217;\u9999\u8549&#8217;, money=2.89, num=30}], 3=[Apple{id=3, name=&#8217;\u8354\u679d&#8217;, money=9.99, num=40}]}<\/p>\n<p>2\u3001List\u8f6cMap<br \/>\nid\u4e3akey\uff0capple\u5bf9\u8c61\u4e3avalue\uff0c\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\n\/**\r\n* List -&gt; Map\r\n* \u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\r\n* toMap \u5982\u679c\u96c6\u5408\u5bf9\u8c61\u6709\u91cd\u590d\u7684key\uff0c\u4f1a\u62a5\u9519Duplicate key ....\r\n* apple1,apple12\u7684id\u90fd\u4e3a1\u3002\r\n* \u53ef\u4ee5\u7528 (k1,k2)-&gt;k1 \u6765\u8bbe\u7f6e\uff0c\u5982\u679c\u6709\u91cd\u590d\u7684key,\u5219\u4fdd\u7559key1,\u820d\u5f03key2\r\n*\/\r\nMap&lt;Integer, Apple&gt; appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -&gt; a,(k1,k2)-&gt;k1));\r\n\r\n<\/pre>\n<p>\u6253\u5370appleMap<br \/>\n{1=Apple{id=1, name=&#8217;\u82f9\u679c1&#8242;, money=3.25, num=10}, 2=Apple{id=2, name=&#8217;\u9999\u8549&#8217;, money=2.89, num=30}, 3=Apple{id=3, name=&#8217;\u8354\u679d&#8217;, money=9.99, num=40}}<\/p>\n<p>3\u3001\u8fc7\u6ee4Filter<br \/>\n\u4ece\u96c6\u5408\u4e2d\u8fc7\u6ee4\u51fa\u6765\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\n\/\/\u8fc7\u6ee4\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\r\nList&lt;Apple&gt; filterList = appleList.stream().filter(a -&gt; a.getName().equals(&quot;\u9999\u8549&quot;)).collect(Collectors.toList());\r\n\r\nSystem.err.println(&quot;filterList:&quot;+filterList);\r\n\r\n<\/pre>\n<p>[Apple{id=2, name=&#8217;\u9999\u8549&#8217;, money=2.89, num=30}]<\/p>\n<p>4.\u6c42\u548c<br \/>\n\u5c06\u96c6\u5408\u4e2d\u7684\u6570\u636e\u6309\u7167\u67d0\u4e2a\u5c5e\u6027\u6c42\u548c:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\n\/\/\u8ba1\u7b97 \u603b\u91d1\u989d\r\nBigDecimal totalMoney = appleList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);\r\nSystem.err.println(&quot;totalMoney:&quot;+totalMoney); \/\/totalMoney:17.48\r\n\r\n<\/pre>\n<p>5.\u67e5\u627e\u6d41\u4e2d\u6700\u5927 \u6700\u5c0f\u503c<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nCollectors.maxBy \u548c Collectors.minBy \u6765\u8ba1\u7b97\u6d41\u4e2d\u7684\u6700\u5927\u6216\u6700\u5c0f\u503c\u3002\r\n\r\nOptional&lt;Dish&gt; maxDish = Dish.menu.stream().\r\ncollect(Collectors.maxBy(Comparator.comparing(Dish::getCalories)));\r\nmaxDish.ifPresent(System.out::println);\r\n\r\nOptional&lt;Dish&gt; minDish = Dish.menu.stream().\r\ncollect(Collectors.minBy(Comparator.comparing(Dish::getCalories)));\r\nminDish.ifPresent(System.out::println);\r\n\r\n<\/pre>\n<p>6.\u53bb\u91cd<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport static java.util.Comparator.comparingLong;\r\nimport static java.util.stream.Collectors.collectingAndThen;\r\nimport static java.util.stream.Collectors.toCollection;\r\n\r\n\/\/ \u6839\u636eid\u53bb\u91cd\r\nList&lt;Person&gt; unique = appleList.stream().collect(\r\ncollectingAndThen(\r\ntoCollection(() -&gt; new TreeSet&lt;&gt;(comparingLong(Apple::getId))), ArrayList::new)\r\n);\r\n\r\n<\/pre>\n<p>\u4e0b\u8868\u5c55\u793a Collectors \u7c7b\u7684\u9759\u6001\u5de5\u5382\u65b9\u6cd5\u3002<\/p>\n<p>\u5de5\u5382\u65b9\u6cd5 \u8fd4\u56de\u7c7b\u578b \u4f5c\u7528<br \/>\ntoList List&lt;T&gt; \u628a\u6d41\u4e2d\u6240\u6709\u9879\u76ee\u6536\u96c6\u5230\u4e00\u4e2a List<br \/>\ntoSet Set&lt;T&gt; \u628a\u6d41\u4e2d\u6240\u6709\u9879\u76ee\u6536\u96c6\u5230\u4e00\u4e2a Set\uff0c\u5220\u9664\u91cd\u590d\u9879<br \/>\ntoCollection Collection&lt;T&gt; \u628a\u6d41\u4e2d\u6240\u6709\u9879\u76ee\u6536\u96c6\u5230\u7ed9\u5b9a\u7684\u4f9b\u5e94\u6e90\u521b\u5efa\u7684\u96c6\u5408menuStream.collect(toCollection(), ArrayList::new)<br \/>\ncounting Long \u8ba1\u7b97\u6d41\u4e2d\u5143\u7d20\u7684\u4e2a\u6570<br \/>\nsumInt Integer \u5bf9\u6d41\u4e2d\u9879\u76ee\u7684\u4e00\u4e2a\u6574\u6570\u5c5e\u6027\u6c42\u548c<br \/>\naveragingInt Double \u8ba1\u7b97\u6d41\u4e2d\u9879\u76ee Integer \u5c5e\u6027\u7684\u5e73\u5747\u503c<br \/>\nsummarizingInt IntSummaryStatistics \u6536\u96c6\u5173\u4e8e\u6d41\u4e2d\u9879\u76ee Integer \u5c5e\u6027\u7684\u7edf\u8ba1\u503c\uff0c\u4f8b\u5982\u6700\u5927\u3001\u6700\u5c0f\u3001 \u603b\u548c\u4e0e\u5e73\u5747\u503c<br \/>\njoining String \u8fde\u63a5\u5bf9\u6d41\u4e2d\u6bcf\u4e2a\u9879\u76ee\u8c03\u7528 toString \u65b9\u6cd5\u6240\u751f\u6210\u7684\u5b57\u7b26\u4e32collect(joining(&#8220;, &#8220;))<br \/>\nmaxBy Optional&lt;T&gt; \u4e00\u4e2a\u5305\u88f9\u4e86\u6d41\u4e2d\u6309\u7167\u7ed9\u5b9a\u6bd4\u8f83\u5668\u9009\u51fa\u7684\u6700\u5927\u5143\u7d20\u7684 Optional\uff0c \u6216\u5982\u679c\u6d41\u4e3a\u7a7a\u5219\u4e3a Optional.empty()<br \/>\nminBy Optional&lt;T&gt; \u4e00\u4e2a\u5305\u88f9\u4e86\u6d41\u4e2d\u6309\u7167\u7ed9\u5b9a\u6bd4\u8f83\u5668\u9009\u51fa\u7684\u6700\u5c0f\u5143\u7d20\u7684 Optional\uff0c \u6216\u5982\u679c\u6d41\u4e3a\u7a7a\u5219\u4e3a Optional.empty()<br \/>\nreducing \u5f52\u7ea6\u64cd\u4f5c\u4ea7\u751f\u7684\u7c7b\u578b \u4ece\u4e00\u4e2a\u4f5c\u4e3a\u7d2f\u52a0\u5668\u7684\u521d\u59cb\u503c\u5f00\u59cb\uff0c\u5229\u7528 BinaryOperator \u4e0e\u6d41 \u4e2d\u7684\u5143\u7d20\u9010\u4e2a\u7ed3\u5408\uff0c\u4ece\u800c\u5c06\u6d41\u5f52\u7ea6\u4e3a\u5355\u4e2a\u503c\u7d2f\u52a0int totalCalories = menuStream.collect(reducing(0, Dish::getCalories, Integer::sum));<br \/>\ncollectingAndThen \u8f6c\u6362\u51fd\u6570\u8fd4\u56de\u7684\u7c7b\u578b \u5305\u88f9\u53e6\u4e00\u4e2a\u6536\u96c6\u5668\uff0c\u5bf9\u5176\u7ed3\u679c\u5e94\u7528\u8f6c\u6362\u51fd\u6570int howManyDishes = menuStream.collect(collectingAndThen(toList(), List::size))<br \/>\ngroupingBy Map&lt;K, List&lt;T&gt;&gt; \u6839\u636e\u9879\u76ee\u7684\u4e00\u4e2a\u5c5e\u6027\u7684\u503c\u5bf9\u6d41\u4e2d\u7684\u9879\u76ee\u4f5c\u95ee\u7ec4\uff0c\u5e76\u5c06\u5c5e\u6027\u503c\u4f5c \u4e3a\u7ed3\u679c Map \u7684\u952e<br \/>\npartitioningBy Map&lt;Boolean,List&lt;T&gt;&gt; \u6839\u636e\u5bf9\u6d41\u4e2d\u6bcf\u4e2a\u9879\u76ee\u5e94\u7528\u8c13\u8bcd\u7684\u7ed3\u679c\u6765\u5bf9\u9879\u76ee\u8fdb\u884c\u5206\u533a<\/p>\n<p>\u6700\u540e\u4e00\u4e2a\u662f\u8868\u683c\uff0c\u53ef\u4ee5\u53c2\u89c1\u539f\u5e16\uff1a<a href=\"https:\/\/blog.csdn.net\/lu930124\/article\/details\/77595585\">https:\/\/blog.csdn.net\/lu930124\/article\/details\/77595585<\/a><\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-614\" src=\"http:\/\/prayerlaputa.com\/wp-content\/uploads\/2019\/08\/\u5e38\u7528collectors-300x204.png\" alt=\"\" width=\"300\" height=\"204\" srcset=\"http:\/\/prayerlaputa.com\/wp-content\/uploads\/2019\/08\/\u5e38\u7528collectors-300x204.png 300w, http:\/\/prayerlaputa.com\/wp-content\/uploads\/2019\/08\/\u5e38\u7528collectors-768x522.png 768w, http:\/\/prayerlaputa.com\/wp-content\/uploads\/2019\/08\/\u5e38\u7528collectors-1024x697.png 1024w, http:\/\/prayerlaputa.com\/wp-content\/uploads\/2019\/08\/\u5e38\u7528collectors.png 1226w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3a\u535a\u4e3b\u539f\u521b\u6587\u7ae0\uff0c\u9075\u5faa CC 4.0 by-sa \u7248\u6743\u534f\u8bae\uff0c\u8f6c\u8f7d\u8bf7\u9644\u4e0a\u539f\u6587\u51fa\u5904\u94fe\u63a5\u548c\u672c\u58f0\u660e\u3002 \u672c\u6587 <a href='http:\/\/prayerlaputa.com\/?p=610' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[6],"tags":[33],"_links":{"self":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/610"}],"collection":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=610"}],"version-history":[{"count":4,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/610\/revisions"}],"predecessor-version":[{"id":615,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/610\/revisions\/615"}],"wp:attachment":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}