{"id":103961,"date":"2020-02-20T23:12:28","date_gmt":"2020-02-20T15:12:28","guid":{"rendered":"http:\/\/4563.org\/?p=103961"},"modified":"2020-02-20T23:12:28","modified_gmt":"2020-02-20T15:12:28","slug":"java8-stream-%e8%af%ad%e6%b3%95%e7%b3%96%e7%9a%84%e5%87%a0%e4%b8%aa%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7%e5%88%86%e4%ba%ab%ef%bc%9a-list-%e8%bd%ac-map%e3%80%81reduce-%e7%ad%89","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=103961","title":{"rendered":"java8 stream \u8bed\u6cd5\u7cd6\u7684\u51e0\u4e2a\u4f7f\u7528\u6280\u5de7\u5206\u4eab\uff1a list \u8f6c map\u3001reduce \u7b49"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  java8 stream \u8bed\u6cd5\u7cd6\u7684\u51e0\u4e2a\u4f7f\u7528\u6280\u5de7\u5206\u4eab\uff1a list \u8f6c map\u3001reduce \u7b49               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : rizon <\/span>  <span><i><\/i> 54<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5206\u4eab\u51e0\u4e2a\u5e73\u5e38\u7528\u5230\u7684 stream \u7684\u4e00\u4e9b\u6280\u5de7<\/p>\n<h3>\u5148\u662f\u6700\u57fa\u672c\u7684\u51e0\u4e2a<\/h3>\n<pre><code> persons.stream()  .map(Person::getName)  .filter(\"notExist\"::equals)  .findAny()  .orElse(\"default\");                         <\/code><\/pre>\n<h3>\u7136\u540e\u662f groupBy \u5b9e\u73b0\u7684 list \u8f6c map<\/h3>\n<p>\u8fd9\u4e2a\u662f\u4e00\u4e2a\u590d\u6742\u4f46\u662f\u5b8c\u6574\u7684\u5199\u6cd5\uff0c\u5176\u5b9e\u53ef\u4ee5\u7b80\u5316\uff0c\u53e6\u5916\u7528 google \u7684 maps \u5de5\u5177\u7c7b\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u3002 \u8be6\u60c5\u53ef\u4ee5\u770bspring \u6700\u4f73\u5b9e\u8df5-Stream \u7684\u4f7f\u7528\u6280\u5de7<\/p>\n<pre><code>public static &lt;T, K&gt; Map&lt;K, T&gt; list2Map(@NonNull Collection&lt;T&gt; list, @NonNull Function&lt;? super T, K&gt; keyFunc) {         return list.stream().collect(Collectors.toMap(keyFunc, Function.identity(),                 (u, v) -&gt; {                     throw new IllegalStateException(String.format(\"Multiple entries with same key,%s=%s,%s=%s\",                             keyFunc.apply(u), u,                             keyFunc.apply(v), v));                 },                 HashMap::new)); } <\/code><\/pre>\n<p><strong>\u867d\u7136\u6211\u5199\u4e86\u8fd9\u4e2a\u5de5\u5177\u7c7b\u4f46\u5176\u5b9e\u5e73\u5e38\u81ea\u5df1\u5e76\u4e0d\u600e\u4e48\u7528\u8fd9\u4e2a\uff0c\u6211\u5e73\u5e38\u90fd\u4f1a\u7528 maps \u5de5\u5177\u7c7b\uff0c\u4f46\u662f\u90a3\u4e2a\u65e0\u6cd5\u4e3b\u52a8\u53bb\u5904\u7406\u91cd\u590d key \u7684\u5f02\u5e38\u53ea\u80fd catch\u3002<\/strong><\/p>\n<h3>\u6700\u540e\u662f reduce \u7684\u4e00\u4e9b\u6280\u5de7<\/h3>\n<pre><code>Person identity = new Person(null, null, 0, null); List&lt;Person&gt; maxAge = persons.stream().collect(         Collectors.collectingAndThen(                 \/\/\u6309\u6027\u522b\u5206\u7ec4                 Collectors.groupingBy(Person::getSex,                         \/\/\u6bcf\u7ec4\u53d6\u5e74\u9f84\u6700\u5927\u7684                         Collectors.reducing(identity, BinaryOperator.maxBy(Comparator.comparing(Person::getAge)))),                 \/\/\u5408\u5e76\u5404\u7ec4\u7684\u503c                 p -&gt; new ArrayList&lt;&gt;(p.values())) ); System.out.println(maxAge); <\/code><\/pre>\n<h4>reduce \u7684\u8bf4\u660e<\/h4>\n<p><code>reduce(accumulator)<\/code> \uff1a\u53c2\u6570\u662f\u4e00\u4e2a\u6267\u884c\u53cc\u76ee\u8fd0\u7b97\u7684 Functional Interface\uff0c\u5047\u5982\u8fd9\u4e2a\u53c2\u6570\u8868\u793a\u7684\u64cd\u4f5c\u4e3a op\uff0cstream \u4e2d\u7684\u5143\u7d20\u4e3a x, y, z, \u2026\uff0c\u5219 reduce() \u6267\u884c\u7684\u5c31\u662f x op y op z &#8230;\uff0c\u6240\u4ee5\u8981\u6c42 op \u8fd9\u4e2a\u64cd\u4f5c\u5177\u6709\u7ed3\u5408\u6027(associative)\uff0c\u5373\u6ee1\u8db3\uff1a (x op y) op z = x op (y op z)\uff0c\u6ee1\u8db3\u8fd9\u4e2a\u8981\u6c42\u7684\u64cd\u4f5c\u4e3b\u8981\u6709\uff1a\u6c42\u548c\u3001\u6c42\u79ef\u3001\u6c42\u6700\u5927\u503c\u3001\u6c42\u6700\u5c0f\u503c\u3001\u5b57\u7b26\u4e32\u8fde\u63a5\u3001\u96c6\u5408\u5e76\u96c6\u548c\u4ea4\u96c6\u7b49\u3002\u53e6\u5916\uff0c\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f Optional \u7684\uff1a<\/p>\n<pre><code>Optional &lt;integer&gt;sum1 = numStream.reduce((x, y) -&gt; x + y); <\/code><\/pre>\n<p><code>reduce(identity, accumulator)<\/code> \uff1a\u53ef\u4ee5\u8ba4\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u9ed8\u8ba4\u503c\uff0c\u4f46\u9700\u8981\u6ee1\u8db3 identity op x = x\uff0c\u6240\u4ee5\u5bf9\u4e8e\u6c42\u548c\u64cd\u4f5c\uff0cidentity \u7684\u503c\u4e3a 0\uff0c\u5bf9\u4e8e\u6c42\u79ef\u64cd\u4f5c\uff0cidentity \u7684\u503c\u4e3a 1\u3002\u8fd4\u56de\u503c\u7c7b\u578b\u662f stream \u5143\u7d20\u7684\u7c7b\u578b\uff1a<\/p>\n<pre><code>Integer sum2 = numStream.reduce(0, Integer::sum); <\/code><\/pre>\n<p>reduce \u5982\u679c\u4e0d\u52a0\u53c2\u6570<code>identity<\/code>\u5219\u8fd4\u56de\u7684\u662f optional \u7c7b\u578b\u7684\uff0creduce \u5728\u8fdb\u884c\u53cc\u76ee\u8fd0\u7b97\u65f6\uff0c\u5176\u4e2d\u4e00\u4e2a\u573a\u666f\u662f\u4e0e<code>identity<\/code>\u505a\u6bd4\u8f83\u64cd\u4f5c\uff0c\u56e0\u6b64\u6211\u4eec\u5e94\u8be5\u6ee1\u8db3<code>identity op x = x<\/code><\/p>\n<h3>\u66f4\u591a\u7cbe\u5f69<\/h3>\n<p><strong>stream \u4f7f\u7528\u7684\u6848\u4f8b\u6e90\u7801\u548c\u6ce8\u91ca\u8bf4\u660e\u89c1\uff1a spring \u6700\u4f73\u5b9e\u8df5-Stream \u7684\u4f7f\u7528\u6280\u5de7<\/strong><\/p>\n<p>\u5176\u5b9e stream \u672c\u8d28\u5c31\u662f\u4e2a\u8bed\u6cd5\u7cd6\uff0c\u5b9e\u73b0\u4e86\u5f88\u591a\u6709\u610f\u601d\u7684\u7279\u6027\uff0c\u8fd8\u63d0\u4f9b\u4e86 parallelStream \u8fd9\u6837\u7684\u7b80\u5355\u5e76\u53d1\u64cd\u4f5c\u3002<\/p>\n<p>\u540e\u7eed\u8fd8\u4f1a\u66f4\u65b0\u66f4\u591a java \u548c Spring \u7684\u4e00\u4e9b\u65e5\u5e38\u6280\u5de7\uff0c      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>16<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"502563\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chendy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd8\u597d\u73b0\u5728 idea \u6709 stream debugger<br \/>\u5426\u5219\u6709\u65f6\u5019\u624b\u6ed1\u5199\u9519\u4e86\u771f\u7684\u4e0d\u597d\u8c03\u8bd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502564\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : winterbells <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Call requires API level 24 (current min is 23)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502565\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : winterbells <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e4b\u524d\u542c\u8bf4\u8c37\u6b4c\u89e3\u51b3\u4e86\u5b89\u5353\u4f4e\u7248\u672c\u8c03\u7528 JDK 8 API \u7684\u95ee\u9898\uff0c\u73b0\u5728\u6ca1\u52a8\u9759\u4e86\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502566\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lululau <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             toMap \u7b2c\u4e09\u4e2a\u53c2\u6570\u4e0d\u52a0\u4f60\u4f1a\u540e\u6094\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502567\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lululau <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             sorry \u770b\u9519\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502568\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cocoCookie <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u8c22\u5206\u4eab                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502569\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lianyue13 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @winterbells Android studio 4.0 \u5f00\u542f desugaring \u5c31\u884c\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502570\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wysnylc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @lululau \u8fd8\u6709 value \u4e3a null \u7684\u5408\u5e76\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502571\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : winterbells <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @lianyue13 \u4e0a\u6b21\u88c5\u4e86 4.0\uff0c\u8fc7\u4e86\u4e00\u9635\u5b50\u68c0\u6d4b\u5347\u7ea7\u53ea\u80fd\u68c0\u6d4b\u5230 beta \u7248 3.6 \u7684\u3002\u3002\u3002\u3002\u516c\u53f8\u7535\u8111\u548c\u81ea\u5df1\u7535\u8111\u90fd\u8fd9\u6837\u3002\u7136\u540e\u5c31\u5220\u4e86(\u256f\u00b0\u25a1\u00b0\uff09\u256f\ufe35 \u253b\u2501\u253b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502572\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lianyue13 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @winterbells \u56e0\u4e3a\u90a3\u6b21 3.6 \u7684\u7248\u672c\u53f7\u6bd4 4.0 \u7684\u9ad8\u4e86\u4e00\u70b9                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502573\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wc951 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9700\u8981\u8fc7\u6ee4\u591a\u4e2a\u6761\u4ef6\u662f\u5199\u5728\u4e00\u4e2a filter \u91cc\u8fd8\u662f\u591a\u4e2a\u6bd4\u8f83\u597d                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502574\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nosilence <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u4ee5\u5199\u4e00\u7bc7\u4e0d\u540c JDK \u5927\u7248\u672c\u4e0b\uff0c\u4e0d\u540c\u6570\u636e\u89c4\u6a21\u7684\u4f7f\u7528 Stream \u548c For \u7684\u6548\u7387\u5bf9\u6bd4                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502575\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : guyeu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u5bf9\u8bed\u6cd5\u7cd6\u53ef\u80fd\u6709\u4e00\u4e9b\u8bef\u89e3\u3002\u3002\u3002lambda \u6216\u8bb8\u662f\u4e2a\u8bed\u6cd5\u7cd6\uff0ctry-resource \u662f\u4e2a\u8bed\u6cd5\u7cd6\uff0c\u6cdb\u578b\u7b97\u534a\u4e2a\u8bed\u6cd5\u7cd6\u3002\u3002\u3002\u4f46\u662f stream \u771f\u4e0d\u662f\u8bed\u6cd5\u7cd6\uff0c\u5b83\u662f\u51fd\u6570\u5f0f\u7f16\u7a0b\u5de5\u5177\u5305\uff0c\u548c\u8bed\u6cd5\u6ca1\u5173\u7cfb\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502576\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hantsy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f5c\u4e3a Stream \u7684 Terminal \u64cd\u4f5c\uff0c\u81f3\u5c11\u8981\u8bb2\u8bb2 Collector \u5427\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502577\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hantsy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Optional \u73b0\u5728\u4e5f\u6709 Stream \u7684\u7279\u6027\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"502578\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : rizon <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @guyeu #13 \u611f\u8c22\u7ea0\u6b63\uff0c\u55ef\uff0c\u8fd9\u91cc\u8bf4\u7684\u786e\u5b9e\u4e0d\u5bf9\uff0clambda \u662f\u8bed\u6cd5\u7cd6\uff0cstream \u4e0d\u662f\u3002jdk8 \u540c\u65f6\u5f15\u5165\u4e86 lambda \u548c stream \u6765\u6ee1\u8db3\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u9700\u6c42                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>java8 stream \u8bed\u6cd5\u7cd6\u7684&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/103961"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=103961"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/103961\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=103961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=103961"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=103961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}