{"id":10020867,"date":"2025-06-30T13:34:26","date_gmt":"2025-06-30T05:34:26","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020867"},"modified":"2025-06-30T13:34:26","modified_gmt":"2025-06-30T05:34:26","slug":"%e3%80%8ajava%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e3%80%8b%ef%bc%9a%e6%8e%8c%e6%8f%a1lambda%e4%b8%8estream%e9%ab%98%e7%ba%a7%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020867","title":{"rendered":"\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u300b\uff1a\u638c\u63e1Lambda\u4e0eStream\u9ad8\u7ea7\u7528\u6cd5"},"content":{"rendered":"<p>\u4ee5\u4e0b\u662fJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2dLambda\u4e0eStream API\u7684\u9ad8\u7ea7\u5e94\u7528\u6307\u5357\uff0c\u7ed3\u54082025\u5e74\u6700\u65b0\u6280\u672f\u5b9e\u8df5\uff1a<\/p>\n<p>\u4e00\u3001Lambda\u8868\u8fbe\u5f0f\u6df1\u5ea6\u4f18\u5316<br \/>\u200c\u7c7b\u578b\u63a8\u65ad\u589e\u5f3a\u200c<\/p>\n<p>\u7701\u7565\u53c2\u6570\u7c7b\u578b\u58f0\u660e\uff0c\u7f16\u8bd1\u5668\u81ea\u52a8\u63a8\u65ad\uff08\u5982(x,y) -&gt; x+y\uff0924<br \/>\u5355\u4e2a\u53c2\u6570\u65f6\u53ef\u7701\u7565\u62ec\u53f7\uff1ax -&gt; x*24<br \/>\u200c\u65b9\u6cd5\u5f15\u7528\u8fdb\u9636\u200c<\/p>\n<p>java<br \/>Copy Code<br \/>\/\/ \u9759\u6001\u65b9\u6cd5\u5f15\u7528<br \/>List&lt;Integer&gt; nums = Arrays.asList(1, 2, 3);<br \/>nums.forEach(System.out::println);  \/\/ \u7b49\u4ef7\u4e8e x -&gt; System.out.println(x):ml-citation{ref=&#8221;5,8&#8243; data=&#8221;citationList&#8221;}<br \/>\u200c\u5f02\u5e38\u5904\u7406\u6280\u5de7\u200c<\/p>\n<p>\u4f7f\u7528Unchecked\u51fd\u6570\u5f0f\u63a5\u53e3\u5305\u88c5\u53d7\u68c0\u5f02\u5e38<br \/>java<br \/>Copy Code<br \/>Function&lt;String, Integer&gt; safeParse = s -&gt; {<\/p>\n<pre><code>try { return Integer.parseInt(s); }\ncatch (NumberFormatException e) { throw new RuntimeException(e); }<\/code><\/pre>\n<p>};<br \/>\u4e8c\u3001Stream API\u9ad8\u9636\u64cd\u4f5c<br \/>\u200c\u64cd\u4f5c\u7c7b\u578b\u200c    \u200c\u5178\u578b\u573a\u666f\u200c    \u200c\u793a\u4f8b\u4ee3\u7801\u200c<br \/>\u200c\u5e76\u884c\u6d41\u4f18\u5316\u200c    \u5927\u6570\u636e\u96c6\u5904\u7406\u52a0\u901f    list.parallelStream().filter(x -&gt; x&gt;0).collect(Collectors.toList()<br \/>\u200c\u77ed\u8def\u7ec8\u6b62\u200c    \u907f\u514d\u5168\u91cf\u904d\u5386    list.stream().anyMatch(x -&gt; x.contains(&#8220;error&#8221;))<br \/>\u200c\u81ea\u5b9a\u4e49\u6536\u96c6\u5668\u200c    \u590d\u6742\u805a\u5408\u903b\u8f91    \u5b9e\u73b0Collector\u63a5\u53e3\u5b9a\u4e49\u5206\u4f4d\u6570\u8ba1\u7b97<br \/>\u200c\u5206\u7ec4\u7edf\u8ba1\u9ad8\u7ea7\u793a\u4f8b\u200c\uff1a<\/p>\n<p>java<br \/>Copy Code<br \/>Map&lt;Department, Double&gt; avgSalary = employees.stream()<\/p>\n<pre><code>.collect(Collectors.groupingBy(\n    Employee::getDept,\n    Collectors.averagingDouble(Employee::getSalary)  \/\/ \u6309\u90e8\u95e8\u8ba1\u7b97\u5e73\u5747\u85aa\u8d44:ml-citation{ref=&quot;10&quot; data=&quot;citationList&quot;}\n));<\/code><\/pre>\n<p>\u4e09\u3001\u6027\u80fd\u9677\u9631\u4e0e\u89e3\u51b3\u65b9\u6848<br \/>\u200cStream\u94fe\u5f0f\u8c03\u7528\u5f00\u9500\u200c<\/p>\n<p>\u5408\u5e76\u540c\u7c7b\u64cd\u4f5c\uff1afilter().map()\u4f18\u4e8emap().filter()<br \/>\u907f\u514d\u91cd\u590d\u521b\u5efaStream\uff1a\u5bf9\u540c\u4e00\u96c6\u5408\u591a\u6b21\u8c03\u7528stream()\u4f1a\u751f\u6210\u65b0\u5bf9\u8c61<br \/>\u200c\u5e76\u884c\u6d41\u6ce8\u610f\u4e8b\u9879\u200c<\/p>\n<p>\u6570\u636e\u91cf\u5c0f\u4e8e1\u4e07\u65f6\u4e32\u884c\u6d41\u66f4\u5feb\uff08\u7ebf\u7a0b\u5207\u6362\u5f00\u9500\uff09<br \/>\u786e\u4fdd\u72b6\u6001\u65e0\u5173\uff1aforEachOrdered\u4fdd\u8bc1\u5e76\u884c\u6d41\u987a\u5e8f\u6027<br \/>\u56db\u3001\u51fd\u6570\u5f0f\u8bbe\u8ba1\u6a21\u5f0f<br \/>\u200c\u7b56\u7565\u6a21\u5f0fLambda\u5316\u200c<\/p>\n<p>java<br \/>Copy Code<br \/>\/\/ \u4f20\u7edf\u63a5\u53e3\u5b9e\u73b0<br \/>interface ValidationStrategy {<\/p>\n<pre><code>boolean execute(String s);<\/code><\/pre>\n<p>}<br \/>\/\/ Lambda\u5b9e\u73b0<br \/>ValidationStrategy numberCheck = s -&gt; s.matches(&#8220;\/d+&#8221;);  \/\/ \u6570\u5b57\u6821\u9a8c\u7b56\u7565:ml-citation{ref=&#8221;7,10&#8243; data=&#8221;citationList&#8221;}<br \/>\u200c\u88c5\u9970\u5668\u6a21\u5f0f\u7ec4\u5408\u200c<\/p>\n<p>java<br \/>Copy Code<br \/>Function&lt;String, String&gt; addHeader = s -&gt; &#8220;Header: &#8221; + s;<br \/>Function&lt;String, String&gt; addFooter = s -&gt; s + &#8221; Footer&#8221;;<br \/>\/\/ \u7ec4\u5408\u88c5\u9970\u5668<br \/>Function&lt;String, String&gt; pipeline = addHeader.andThen(addFooter);:ml-citation{ref=&#8221;10&#8243; data=&#8221;citationList&#8221;}<br \/>\u4e94\u3001\u6700\u65b0\u6280\u672f\u52a8\u6001\uff08JDK24\uff09<br \/>\u200cStream\u589e\u5f3a\u200c<\/p>\n<p>gather()\u64cd\u4f5c\u7b26\u652f\u6301\u81ea\u5b9a\u4e49\u4e2d\u95f4\u64cd\u4f5c<br \/>\u65b0\u589emapMulti\u66ff\u4ee3flatMap\u5904\u7406\u5d4c\u5957\u96c6\u5408<br \/>\u200c\u6a21\u5f0f\u5339\u914d\u6269\u5c55\u200c<\/p>\n<p>java<br \/>Copy Code<br \/>Object obj = &#8220;Hello&#8221;;<br \/>String result = switch(obj) {<\/p>\n<pre><code>case String s when s.length()&gt;5 -&gt; &quot;Long string&quot;;  \/\/ \u6a21\u5f0f\u5339\u914d+\u6761\u4ef6\u5224\u65ad:ml-citation{ref=&quot;9&quot; data=&quot;citationList&quot;}\ndefault -&gt; &quot;Other&quot;;<\/code><\/pre>\n<p>};<br \/>\u200c\u6700\u4f73\u5b9e\u8df5\u200c\uff1a\u5728\u91d1\u878d\u4ea4\u6613\u7cfb\u7edf\u4e2d\uff0cLambda+Stream\u53ef\u4f7f\u6838\u5fc3\u903b\u8f91\u4ee3\u7801\u91cf\u51cf\u5c1140%\uff0c\u540c\u65f6\u63d0\u5347\u53ef\u7ef4\u62a4\u6027\u3002\u63a8\u8350\u4f7f\u7528@FunctionalInterface\u6ce8\u89e3\u660e\u786e\u63a5\u53e3\u8bbe\u8ba1\u610f\u56fe\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046806309\">https:\/\/segmentfault.com\/a\/1190000046806309<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662fJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2dLambda\u4e0eStream API\u7684\u9ad8\u7ea7\u5e94\u7528\u6307\u5357\uff0c\u7ed3\u54082025\u5e74\u6700\u65b0\u6280\u672f\u5b9e\u8df5\uff1a \u4e00\u3001 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020867","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020867"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020867\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}