{"id":73280,"date":"2026-02-07T11:26:01","date_gmt":"2026-02-07T03:26:01","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/73280.html"},"modified":"2026-02-07T11:26:01","modified_gmt":"2026-02-07T03:26:01","slug":"java-8-%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e5%85%a5%e9%97%a8%ef%bc%9a%e4%bb%8e-lambda-%e5%88%b0-stream-%e5%b9%b6%e8%a1%8c%e6%89%a7%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/73280.html","title":{"rendered":"Java 8 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5165\u95e8\uff1a\u4ece Lambda \u5230 Stream \u5e76\u884c\u6267\u884c"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"1.%20Lambda%20%E8%A1%A8%E8%BE%BE%E5%BC%8F%EF%BC%9A%E5%B0%86%E2%80%9C%E8%A1%8C%E4%B8%BA%E9%80%BB%E8%BE%91%E2%80%9D%E4%BD%9C%E4%B8%BA%E5%8F%82%E6%95%B0%E4%BC%A0%E9%80%92-toc\" style=\"margin-left:0px\">1. Lambda \u8868\u8fbe\u5f0f&#xff1a;\u5c06\u201c\u884c\u4e3a\u903b\u8f91\u201d\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012<\/p>\n<p id=\"2.%20Stream%20API%EF%BC%9A%E5%9F%BA%E4%BA%8E%E6%B5%81%E6%B0%B4%E7%BA%BF%E6%A8%A1%E5%9E%8B%E7%9A%84%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86%E6%9C%BA%E5%88%B6%EF%BC%88%E8%AF%A6%E8%A7%A3%EF%BC%89-toc\" style=\"margin-left:0px\">2. Stream API&#xff1a;\u57fa\u4e8e\u6d41\u6c34\u7ebf\u6a21\u578b\u7684\u6570\u636e\u5904\u7406\u673a\u5236&#xff08;\u8be6\u89e3&#xff09;<\/p>\n<p id=\"2.1%20Stream%20%E7%9A%84%E6%A0%B8%E5%BF%83%E6%80%9D%E6%83%B3%EF%BC%9A%E6%8F%8F%E8%BF%B0%E2%80%9C%E6%95%B0%E6%8D%AE%E5%A6%82%E4%BD%95%E8%A2%AB%E5%A4%84%E7%90%86%E2%80%9D-toc\" style=\"margin-left:40px\">2.1 Stream \u7684\u6838\u5fc3\u601d\u60f3&#xff1a;\u63cf\u8ff0\u201c\u6570\u636e\u5982\u4f55\u88ab\u5904\u7406\u201d<\/p>\n<p id=\"2.2%20Stream%20%E7%9A%84%E4%B8%89%E5%A4%A7%E7%BB%84%E6%88%90%E9%83%A8%E5%88%86-toc\" style=\"margin-left:40px\">2.2 Stream \u7684\u4e09\u5927\u7ec4\u6210\u90e8\u5206<\/p>\n<p id=\"2.2.1%20%E6%95%B0%E6%8D%AE%E6%BA%90%EF%BC%88Source%EF%BC%89-toc\" style=\"margin-left:80px\">2.2.1 \u6570\u636e\u6e90&#xff08;Source&#xff09;<\/p>\n<p id=\"2.2.2%20%E4%B8%AD%E9%97%B4%E6%93%8D%E4%BD%9C%EF%BC%88Intermediate%20Operation%EF%BC%89-toc\" style=\"margin-left:80px\">2.2.2 \u4e2d\u95f4\u64cd\u4f5c&#xff08;Intermediate Operation&#xff09;<\/p>\n<p id=\"2.2.3%20%E7%BB%88%E6%AD%A2%E6%93%8D%E4%BD%9C%EF%BC%88Terminal%20Operation%EF%BC%89-toc\" style=\"margin-left:80px\">2.2.3 \u7ec8\u6b62\u64cd\u4f5c&#xff08;Terminal Operation&#xff09;<\/p>\n<p id=\"2.3%20%E6%83%B0%E6%80%A7%E6%89%A7%E8%A1%8C%E4%B8%8E%E6%B5%81%E6%B0%B4%E7%BA%BF%E4%BC%98%E5%8C%96%E6%9C%BA%E5%88%B6-toc\" style=\"margin-left:40px\">2.3 \u60f0\u6027\u6267\u884c\u4e0e\u6d41\u6c34\u7ebf\u4f18\u5316\u673a\u5236<\/p>\n<p id=\"2.4%20%E4%B8%B2%E8%A1%8C%E6%B5%81%E4%B8%8E%E5%B9%B6%E8%A1%8C%E6%B5%81-toc\" style=\"margin-left:40px\">2.4 \u4e32\u884c\u6d41\u4e0e\u5e76\u884c\u6d41<\/p>\n<p id=\"%EF%BC%881%EF%BC%89%E4%B8%B2%E8%A1%8C%E6%B5%81%EF%BC%88Sequential%20Stream%EF%BC%89-toc\" style=\"margin-left:80px\">2.4.1 \u4e32\u884c\u6d41&#xff08;Sequential Stream&#xff09;<\/p>\n<p id=\"%EF%BC%882%EF%BC%89%E5%B9%B6%E8%A1%8C%E6%B5%81%EF%BC%88Parallel%20Stream%EF%BC%89-toc\" style=\"margin-left:80px\">2.4.2\u00a0\u5e76\u884c\u6d41&#xff08;Parallel Stream&#xff09;<\/p>\n<p id=\"a.%E4%BB%80%E4%B9%88%E6%98%AF%20Fork%2FJoin%20%E6%A1%86%E6%9E%B6%EF%BC%9F-toc\" style=\"margin-left:120px\">a.\u4ec0\u4e48\u662f Fork\/Join \u6846\u67b6&#xff1f;<\/p>\n<p id=\"b.%E5%B9%B6%E8%A1%8C%E6%B5%81%E7%9A%84%E6%89%A7%E8%A1%8C%E6%B5%81%E7%A8%8B-toc\" style=\"margin-left:120px\">b.\u5e76\u884c\u6d41\u7684\u6267\u884c\u6d41\u7a0b<\/p>\n<p id=\"c.Fork%2FJoin%20%E4%B8%8E%E5%B9%B6%E8%A1%8C%E6%B5%81%E7%9A%84%E5%85%B3%E7%B3%BB-toc\" style=\"margin-left:120px\">c.Fork\/Join \u4e0e\u5e76\u884c\u6d41\u7684\u5173\u7cfb<\/p>\n<p id=\"3.%20Optional%EF%BC%9A%E6%98%BE%E5%BC%8F%E8%A1%A8%E8%BE%BE%E2%80%9C%E5%80%BC%E5%8F%AF%E8%83%BD%E4%B8%8D%E5%AD%98%E5%9C%A8%E2%80%9D%E7%9A%84%E8%AF%AD%E4%B9%89-toc\" style=\"margin-left:0px\">3. Optional&#xff1a;\u663e\u5f0f\u8868\u8fbe\u201c\u503c\u53ef\u80fd\u4e0d\u5b58\u5728\u201d\u7684\u8bed\u4e49<\/p>\n<hr id=\"hr-toc\" \/>\n<hr \/>\n<p>Java 8 \u5f15\u5165\u4e86 Lambda \u8868\u8fbe\u5f0f\u3001Stream API \u548c Optional&#xff0c;\u4e3a\u96c6\u5408\u6570\u636e\u5904\u7406\u548c\u7a7a\u503c\u7ba1\u7406\u63d0\u4f9b\u4e86\u66f4\u52a0\u7b80\u6d01\u4e14\u5b89\u5168\u7684\u7f16\u7a0b\u65b9\u5f0f\u3002Lambda \u7528\u4e8e\u63cf\u8ff0\u53ef\u4f20\u9012\u7684\u884c\u4e3a\u903b\u8f91&#xff0c;Stream API \u901a\u8fc7\u6d41\u6c34\u7ebf\u6a21\u578b\u5bf9\u6570\u636e\u8fdb\u884c\u58f0\u660e\u5f0f\u5904\u7406&#xff0c;\u800c Optional \u5219\u663e\u5f0f\u5efa\u6a21\u201c\u53ef\u80fd\u4e3a\u7a7a\u201d\u7684\u503c&#xff0c;\u4ece\u800c\u964d\u4f4e\u7a7a\u6307\u9488\u5f02\u5e38\u7684\u98ce\u9669\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0\">\u5728\u6267\u884c\u5c42\u9762&#xff0c;Stream API \u540c\u65f6\u652f\u6301\u4e32\u884c\u6d41\u4e0e\u5e76\u884c\u6d41\u3002\u4e32\u884c\u6d41\u5f3a\u8c03\u987a\u5e8f\u6027\u4e0e\u53ef\u9884\u6d4b\u6027&#xff0c;\u5e76\u884c\u6d41\u5219\u57fa\u4e8e\u591a\u7ebf\u7a0b\u673a\u5236\u63d0\u5347\u8ba1\u7b97\u6548\u7387&#xff0c;\u9002\u7528\u4e8e CPU \u5bc6\u96c6\u578b\u573a\u666f\u3002\u4e8c\u8005\u5728\u4fdd\u6301\u7edf\u4e00\u7f16\u7a0b\u6a21\u578b\u7684\u540c\u65f6&#xff0c;\u4e3a\u4e0d\u540c\u4e1a\u52a1\u9700\u6c42\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u6267\u884c\u9009\u62e9\u3002<\/p>\n<hr \/>\n<h2 id=\"1.%20Lambda%20%E8%A1%A8%E8%BE%BE%E5%BC%8F%EF%BC%9A%E5%B0%86%E2%80%9C%E8%A1%8C%E4%B8%BA%E9%80%BB%E8%BE%91%E2%80%9D%E4%BD%9C%E4%B8%BA%E5%8F%82%E6%95%B0%E4%BC%A0%E9%80%92\" style=\"margin-left:0px;margin-right:0px\">1. Lambda \u8868\u8fbe\u5f0f&#xff1a;\u5c06\u201c\u884c\u4e3a\u903b\u8f91\u201d\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012<\/h2>\n<p style=\"margin-left:0;margin-right:0\">\u5728\u4f20\u7edf Java \u7f16\u7a0b\u4e2d&#xff0c;\u65b9\u6cd5\u53c2\u6570\u901a\u5e38\u53ea\u80fd\u4f20\u9012\u6570\u636e&#xff0c;\u800c\u65e0\u6cd5\u76f4\u63a5\u4f20\u9012\u201c\u8981\u6267\u884c\u7684\u64cd\u4f5c\u201d\u3002\u5f53\u9700\u8981\u5c06\u4e00\u6bb5\u5904\u7406\u903b\u8f91\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u65b9\u6cd5\u65f6&#xff0c;\u5f80\u5f80\u53ea\u80fd\u901a\u8fc7\u533f\u540d\u5185\u90e8\u7c7b\u7684\u65b9\u5f0f\u5b9e\u73b0&#xff0c;\u8fd9\u4e0d\u4ec5\u4ee3\u7801\u5197\u957f&#xff0c;\u4e5f\u524a\u5f31\u4e86\u903b\u8f91\u672c\u8eab\u7684\u53ef\u8bfb\u6027\u3002Lambda \u8868\u8fbe\u5f0f\u7684\u5f15\u5165&#xff0c;\u6b63\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0\">\u8fd9\u91cc\u6240\u8bf4\u7684\u201c\u884c\u4e3a\u903b\u8f91\u201d&#xff0c;\u672c\u8d28\u4e0a\u662f\u6307\u5bf9\u67d0\u4e2a\u8f93\u5165\u6267\u884c\u7684\u64cd\u4f5c\u89c4\u5219&#xff0c;\u4f8b\u5982&#xff1a;\u5982\u4f55\u5904\u7406\u4e00\u4e2a\u5143\u7d20\u3001\u5982\u4f55\u8fdb\u884c\u5224\u65ad\u3001\u5982\u4f55\u5b8c\u6210\u4e00\u6b21\u8ba1\u7b97\u7b49\u3002Lambda \u5141\u8bb8\u5c06\u8fd9\u4e9b\u64cd\u4f5c\u89c4\u5219\u4ee5\u51fd\u6570\u7684\u5f62\u5f0f\u8fdb\u884c\u4f20\u9012&#xff0c;\u4f7f\u4ee3\u7801\u80fd\u591f\u76f4\u63a5\u8868\u8fbe\u201c\u5bf9\u6570\u636e\u505a\u4ec0\u4e48\u201d\u3002<\/p>\n<p>\/\/ \u533f\u540d\u5185\u90e8\u7c7b&#xff1a;\u4f20\u9012\u884c\u4e3a\u903b\u8f91<br \/>\nlist.forEach(new Consumer&lt;Integer&gt;() {<br \/>\n    &#064;Override<br \/>\n    public void accept(Integer x) {<br \/>\n        System.out.println(x);<br \/>\n    }<br \/>\n});<\/p>\n<p>\/\/ Lambda&#xff1a;\u66f4\u76f4\u89c2\u5730\u63cf\u8ff0\u884c\u4e3a<br \/>\nlist.forEach(x -&gt; System.out.println(x));<\/p>\n<p style=\"margin-left:0;margin-right:0\">Lambda \u8868\u8fbe\u5f0f\u5fc5\u987b\u4f9d\u9644\u4e8e\u51fd\u6570\u5f0f\u63a5\u53e3&#xff08;Functional Interface&#xff09; \u624d\u80fd\u4f7f\u7528\u3002\u6240\u8c13\u51fd\u6570\u5f0f\u63a5\u53e3&#xff0c;\u662f\u6307\u53ea\u5305\u542b\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\u7684\u63a5\u53e3&#xff0c;\u4f8b\u5982 Runnable\u3001Comparator\u3001Consumer \u7b49\u3002Lambda \u5e76\u4e0d\u662f\u8131\u79bb\u63a5\u53e3\u5b58\u5728\u7684\u51fd\u6570&#xff0c;\u800c\u662f\u51fd\u6570\u5f0f\u63a5\u53e3\u7684\u7b80\u6d01\u5b9e\u73b0\u5f62\u5f0f\u3002<\/p>\n<p>&#064;FunctionalInterface<br \/>\npublic interface Calculator {<br \/>\n    int add(int a, int b);<br \/>\n}<\/p>\n<p>\/\/ Lambda \u5b9e\u73b0\u51fd\u6570\u5f0f\u63a5\u53e3<br \/>\nCalculator calc &#061; (a, b) -&gt; a &#043; b;<\/p>\n<p>\u901a\u8fc7 Lambda&#xff0c;Java \u5728\u4fdd\u6301\u7c7b\u578b\u5b89\u5168\u7684\u524d\u63d0\u4e0b&#xff0c;\u5b9e\u73b0\u4e86\u5bf9\u201c\u884c\u4e3a\u62bd\u8c61\u201d\u7684\u7b80\u5316\u8868\u8fbe&#xff0c;\u4e3a\u540e\u7eed Stream API \u7684\u8bbe\u8ba1\u5960\u5b9a\u4e86\u57fa\u7840\u3002<\/p>\n<p>\u63d0\u9192&#xff1a;\u7c7b\u578b\u5b89\u5168\u6307\u7684\u662f<\/p>\n<p>\u7a0b\u5e8f\u5728\u7f16\u8bd1\u671f\u548c\u8fd0\u884c\u671f&#xff0c;\u80fd\u591f\u4fdd\u8bc1\u53d8\u91cf\u3001\u8868\u8fbe\u5f0f\u548c\u65b9\u6cd5\u8c03\u7528\u53ea\u4f1a\u4ee5\u201c\u7b26\u5408\u5176\u58f0\u660e\u7c7b\u578b\u201d\u7684\u65b9\u5f0f\u88ab\u4f7f\u7528&#xff0c;\u4ece\u800c\u907f\u514d\u4e0d\u5408\u6cd5\u7684\u7c7b\u578b\u64cd\u4f5c\u3002<\/p>\n<p>\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u4f8b\u5b50&#xff1a;<\/p>\n<p>String s &#061; &#034;hello&#034;;<br \/>\nInteger i &#061; s;   \/\/ \u7f16\u8bd1\u671f\u76f4\u63a5\u62a5\u9519<\/p>\n<hr \/>\n<h2 id=\"2.%20Stream%20API%EF%BC%9A%E5%9F%BA%E4%BA%8E%E6%B5%81%E6%B0%B4%E7%BA%BF%E6%A8%A1%E5%9E%8B%E7%9A%84%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86%E6%9C%BA%E5%88%B6%EF%BC%88%E8%AF%A6%E8%A7%A3%EF%BC%89\" style=\"margin-left:0px;margin-right:0px\">2. Stream API&#xff1a;\u57fa\u4e8e\u6d41\u6c34\u7ebf\u6a21\u578b\u7684\u6570\u636e\u5904\u7406\u673a\u5236&#xff08;\u8be6\u89e3&#xff09;<\/h2>\n<h3 id=\"2.1%20Stream%20%E7%9A%84%E6%A0%B8%E5%BF%83%E6%80%9D%E6%83%B3%EF%BC%9A%E6%8F%8F%E8%BF%B0%E2%80%9C%E6%95%B0%E6%8D%AE%E5%A6%82%E4%BD%95%E8%A2%AB%E5%A4%84%E7%90%86%E2%80%9D\" style=\"margin-left:0px;margin-right:0px\">2.1 Stream \u7684\u6838\u5fc3\u601d\u60f3&#xff1a;\u63cf\u8ff0\u201c\u6570\u636e\u5982\u4f55\u88ab\u5904\u7406\u201d<\/h3>\n<p style=\"margin-left:0;margin-right:0\">Stream API \u5e76\u4e0d\u662f\u4e00\u79cd\u65b0\u7684\u96c6\u5408\u7ed3\u6784&#xff0c;\u800c\u662f\u5bf9\u96c6\u5408\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u7684\u4e00\u79cd\u62bd\u8c61\u63cf\u8ff0\u3002\u5b83\u7684\u8bbe\u8ba1\u76ee\u6807\u5e76\u975e\u66ff\u4ee3 List \u6216 Set&#xff0c;\u800c\u662f\u5c06\u539f\u672c\u5206\u6563\u5728\u591a\u5c42\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\u4e2d\u7684\u903b\u8f91&#xff0c;\u7ec4\u7ec7\u4e3a\u4e00\u6761\u6e05\u6670\u3001\u53ef\u7ec4\u5408\u7684\u6570\u636e\u5904\u7406\u6d41\u6c34\u7ebf\u3002<\/p>\n<p>\/\/ \u4f20\u7edf\u5199\u6cd5&#xff1a;\u591a\u5c42\u5faa\u73af &#043; \u6761\u4ef6\u5224\u65ad&#xff08;\u903b\u8f91\u5206\u6563&#xff09;<br \/>\nList&lt;Long&gt; result &#061; new ArrayList&lt;&gt;();<\/p>\n<p>for (Order order : orders) {<br \/>\n    if (order.isPaid()) {<br \/>\n        if (order.getAmount() &gt; 100) {<br \/>\n            if (!blackList.contains(order.getUserId())) {<br \/>\n                result.add(order.getId());<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p style=\"margin-left:0;margin-right:0\">\u4ece\u4f7f\u7528\u89d2\u5ea6\u6765\u770b&#xff0c;Stream \u66f4\u5173\u6ce8\u201c\u5bf9\u6570\u636e\u505a\u4ec0\u4e48\u201d&#xff0c;\u800c\u4e0d\u662f\u201c\u5982\u4f55\u904d\u5386\u6570\u636e\u201d\u3002\u8fd9\u79cd\u58f0\u660e\u5f0f\u98ce\u683c\u4f7f\u5f97\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u903b\u8f91\u80fd\u591f\u4ee5\u63a5\u8fd1\u81ea\u7136\u8bed\u8a00\u7684\u65b9\u5f0f\u8868\u8fbe\u3002<\/p>\n<p>users.stream()<br \/>\n     .filter(u -&gt; u.getAge() &gt;&#061; 18)<br \/>\n     .map(User::getName)<br \/>\n     .sorted()<br \/>\n     .forEach(System.out::println);<\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u5e76\u672a\u663e\u5f0f\u51fa\u73b0\u5faa\u73af\u3001\u7d22\u5f15\u6216\u4e2d\u95f4\u53d8\u91cf&#xff0c;\u4f46\u5b8c\u6574\u5730\u63cf\u8ff0\u4e86\u6570\u636e\u7684\u5904\u7406\u89c4\u5219\u3002<\/p>\n<h3 id=\"2.2%20Stream%20%E7%9A%84%E4%B8%89%E5%A4%A7%E7%BB%84%E6%88%90%E9%83%A8%E5%88%86\">2.2 Stream \u7684\u4e09\u5927\u7ec4\u6210\u90e8\u5206<\/h3>\n<p>\u4ece\u7ed3\u6784\u4e0a\u770b&#xff0c;\u4e00\u4e2a\u5b8c\u6574\u7684 Stream \u7531 \u6570\u636e\u6e90&#xff08;Source&#xff09;\u3001\u4e2d\u95f4\u64cd\u4f5c&#xff08;Intermediate Operation&#xff09; \u548c \u7ec8\u6b62\u64cd\u4f5c&#xff08;Terminal Operation&#xff09; \u4e09\u90e8\u5206\u6784\u6210\u3002<\/p>\n<h4 id=\"2.2.1%20%E6%95%B0%E6%8D%AE%E6%BA%90%EF%BC%88Source%EF%BC%89\">2.2.1 \u6570\u636e\u6e90&#xff08;Source&#xff09;<\/h4>\n<p>\u6570\u636e\u6e90\u7528\u4e8e\u751f\u6210 Stream&#xff0c;\u5e38\u89c1\u6765\u6e90\u5305\u62ec\u96c6\u5408\u3001\u6570\u7ec4\u4ee5\u53ca\u5176\u4ed6 I\/O \u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p>list.stream();<br \/>\nArrays.stream(arr);<\/p>\n<p>\u6570\u636e\u6e90\u672c\u8eab\u5e76\u4e0d\u4f1a\u89e6\u53d1\u4efb\u4f55\u8ba1\u7b97&#xff0c;\u4ec5\u7528\u4e8e\u5b9a\u4e49\u6570\u636e\u7684\u8d77\u70b9\u3002<\/p>\n<h4 id=\"2.2.2%20%E4%B8%AD%E9%97%B4%E6%93%8D%E4%BD%9C%EF%BC%88Intermediate%20Operation%EF%BC%89\">2.2.2 \u4e2d\u95f4\u64cd\u4f5c&#xff08;Intermediate Operation&#xff09;<\/h4>\n<p>\u4e2d\u95f4\u64cd\u4f5c\u7528\u4e8e\u63cf\u8ff0\u6570\u636e\u7684\u8f6c\u6362\u89c4\u5219&#xff0c;\u4f8b\u5982\u8fc7\u6ee4\u3001\u6620\u5c04\u3001\u6392\u5e8f\u7b49\u3002\u4e2d\u95f4\u64cd\u4f5c\u7684\u4e00\u4e2a\u5173\u952e\u7279\u5f81\u662f\u60f0\u6027\u6267\u884c&#xff1a;\u5b83\u4eec\u53ea\u4f1a\u8bb0\u5f55\u64cd\u4f5c\u903b\u8f91&#xff0c;\u800c\u4e0d\u4f1a\u7acb\u5373\u6267\u884c\u3002<\/p>\n<p>Stream&lt;Integer&gt; s &#061; list.stream()<br \/>\n        .filter(x -&gt; x &gt; 10)<br \/>\n        .map(x -&gt; x * 2);<\/p>\n<p>\u5728\u6ca1\u6709\u7ec8\u6b62\u64cd\u4f5c\u4e4b\u524d&#xff0c;\u4e0a\u8ff0\u4ee3\u7801\u4e0d\u4f1a\u5bf9\u6570\u636e\u8fdb\u884c\u4efb\u4f55\u5b9e\u9645\u5904\u7406\u3002\u5e38\u89c1\u7684\u4e2d\u95f4\u64cd\u4f5c\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>filter&#xff1a;\u7b5b\u9009\u5143\u7d20<\/li>\n<li>map \/ flatMap&#xff1a;\u6570\u636e\u8f6c\u6362<\/li>\n<li>distinct&#xff1a;\u53bb\u91cd<\/li>\n<li>sorted&#xff1a;\u6392\u5e8f<\/li>\n<li>limit \/ skip&#xff1a;\u622a\u53d6\u6570\u636e<\/li>\n<\/ul>\n<h4 id=\"2.2.3%20%E7%BB%88%E6%AD%A2%E6%93%8D%E4%BD%9C%EF%BC%88Terminal%20Operation%EF%BC%89\">2.2.3 \u7ec8\u6b62\u64cd\u4f5c&#xff08;Terminal Operation&#xff09;<\/h4>\n<p>\u7ec8\u6b62\u64cd\u4f5c\u7528\u4e8e\u89e6\u53d1 Stream \u7684\u6267\u884c&#xff0c;\u5e76\u751f\u6210\u6700\u7ec8\u7ed3\u679c\u3002\u4e00\u65e6\u7ec8\u6b62\u64cd\u4f5c\u88ab\u8c03\u7528&#xff0c;Stream \u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u4e2d\u95f4\u64cd\u4f5c\u5c06\u88ab\u4e00\u6b21\u6027\u6267\u884c\u3002<\/p>\n<p>long count &#061; list.stream()<br \/>\n        .filter(x -&gt; x &gt; 10)<br \/>\n        .count();<\/p>\n<p style=\"margin-left:0;margin-right:0\">\u5e38\u89c1\u7ec8\u6b62\u64cd\u4f5c\u5305\u62ec forEach\u3001collect\u3001count\u3001reduce \u7b49\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;Stream \u53ea\u80fd\u88ab\u6d88\u8d39\u4e00\u6b21&#xff0c;\u7ec8\u6b62\u64cd\u4f5c\u6267\u884c\u5b8c\u6210\u540e&#xff0c;Stream \u5373\u8fdb\u5165\u5173\u95ed\u72b6\u6001\u3002<\/p>\n<h3 id=\"2.3%20%E6%83%B0%E6%80%A7%E6%89%A7%E8%A1%8C%E4%B8%8E%E6%B5%81%E6%B0%B4%E7%BA%BF%E4%BC%98%E5%8C%96%E6%9C%BA%E5%88%B6\">2.3 \u60f0\u6027\u6267\u884c\u4e0e\u6d41\u6c34\u7ebf\u4f18\u5316\u673a\u5236<\/h3>\n<p>Stream API \u91c7\u7528\u60f0\u6027\u6267\u884c\u673a\u5236&#xff0c;\u5176\u6838\u5fc3\u4f18\u52bf\u5728\u4e8e\u80fd\u591f\u5bf9\u6574\u4e2a\u64cd\u4f5c\u94fe\u8fdb\u884c\u7edf\u4e00\u8c03\u5ea6\u4e0e\u4f18\u5316\u3002\u6240\u8c13\u60f0\u6027\u6267\u884c&#xff0c;\u662f\u6307 Stream \u7684\u4e2d\u95f4\u64cd\u4f5c\u5e76\u4e0d\u4f1a\u5728\u58f0\u660e\u65f6\u7acb\u5373\u6267\u884c&#xff0c;\u800c\u662f\u5ef6\u8fdf\u5230\u7ec8\u6b62\u64cd\u4f5c\u89e6\u53d1\u65f6\u7edf\u4e00\u6267\u884c\u3002\u8fd9\u4e00\u8bbe\u8ba1\u4f7f\u5f97 JVM \u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u5bf9\u5b8c\u6574\u7684\u64cd\u4f5c\u6d41\u6c34\u7ebf\u8fdb\u884c\u5206\u6790&#xff0c;\u4ece\u800c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u4e2d\u95f4\u7ed3\u679c\u751f\u6210\u3002<\/p>\n<p>list.stream()<br \/>\n    .filter(x -&gt; x &gt; 10)<br \/>\n    .map(x -&gt; x * 2)<br \/>\n    .forEach(System.out::println);<\/p>\n<p style=\"margin-left:0;margin-right:0\">\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d&#xff0c;filter \u4e0e map \u5747\u4e3a\u4e2d\u95f4\u64cd\u4f5c&#xff0c;\u5b83\u4eec\u4ec5\u7528\u4e8e\u63cf\u8ff0\u6570\u636e\u5904\u7406\u89c4\u5219&#xff0c;\u5e76\u4e0d\u4f1a\u72ec\u7acb\u6267\u884c\u3002\u53ea\u6709\u5728\u8c03\u7528\u7ec8\u6b62\u64cd\u4f5c forEach \u540e&#xff0c;\u6574\u4e2a Stream \u624d\u4f1a\u88ab\u771f\u6b63\u6d88\u8d39\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0\">\u5728\u5e95\u5c42\u6267\u884c\u8fc7\u7a0b\u4e2d&#xff0c;Stream \u4e0d\u4f1a\u5148\u751f\u6210\u4e00\u4e2a\u201c\u8fc7\u6ee4\u540e\u7684\u96c6\u5408\u201d&#xff0c;\u518d\u751f\u6210\u4e00\u4e2a\u201c\u6620\u5c04\u540e\u7684\u96c6\u5408\u201d&#xff0c;\u800c\u662f\u91c7\u7528\u6d41\u6c34\u7ebf\u878d\u5408&#xff08;Pipeline Fusion&#xff09;\u7684\u65b9\u5f0f&#xff0c;\u5bf9\u6570\u636e\u8fdb\u884c\u9010\u5143\u7d20\u5904\u7406\u3002\u5177\u4f53\u800c\u8a00&#xff0c;\u6bcf\u4e2a\u5143\u7d20\u5728\u4e00\u6b21\u904d\u5386\u8fc7\u7a0b\u4e2d\u4f9d\u6b21\u5b8c\u6210 filter \u2192 map \u2192 forEach \u7684\u64cd\u4f5c\u94fe&#xff0c;\u4ece\u800c\u907f\u514d\u591a\u6b21\u904d\u5386\u548c\u4e2d\u95f4\u96c6\u5408\u521b\u5efa\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0\">\u8fd9\u79cd\u6d41\u6c34\u7ebf\u6267\u884c\u6a21\u578b\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf&#xff1a;<\/p>\n<ul>\n<li>\u51cf\u5c11\u4e2d\u95f4\u5bf9\u8c61\u521b\u5efa&#xff0c;\u964d\u4f4e\u5185\u5b58\u5f00\u9500<\/li>\n<li>\u907f\u514d\u591a\u6b21\u904d\u5386\u96c6\u5408&#xff0c;\u63d0\u9ad8\u6267\u884c\u6548\u7387<\/li>\n<li>\u4e3a\u540e\u7eed\u7684\u77ed\u8def\u4f18\u5316&#xff08;\u5982 findFirst\u3001anyMatch&#xff09;\u63d0\u4f9b\u57fa\u7840<\/li>\n<\/ul>\n<p style=\"margin-left:0;margin-right:0\">\u901a\u8fc7\u60f0\u6027\u6267\u884c\u4e0e\u6d41\u6c34\u7ebf\u878d\u5408\u673a\u5236&#xff0c;Stream API \u5728\u4fdd\u8bc1\u4ee3\u7801\u8868\u8fbe\u80fd\u529b\u7684\u540c\u65f6&#xff0c;\u5b9e\u73b0\u4e86\u8f83\u9ad8\u7684\u8fd0\u884c\u6548\u7387\u3002<\/p>\n<h3 id=\"2.4%20%E4%B8%B2%E8%A1%8C%E6%B5%81%E4%B8%8E%E5%B9%B6%E8%A1%8C%E6%B5%81\" style=\"margin-left:0px;margin-right:0px\">2.4 \u4e32\u884c\u6d41\u4e0e\u5e76\u884c\u6d41<\/h3>\n<h4 id=\"%EF%BC%881%EF%BC%89%E4%B8%B2%E8%A1%8C%E6%B5%81%EF%BC%88Sequential%20Stream%EF%BC%89\" style=\"margin-left:0px;margin-right:0px\">2.4.1 \u4e32\u884c\u6d41&#xff08;Sequential Stream&#xff09;<\/h4>\n<p style=\"margin-left:0;margin-right:0\">\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;Stream \u4ee5\u4e32\u884c\u65b9\u5f0f\u6267\u884c&#xff0c;\u7531\u5355\u7ebf\u7a0b\u987a\u5e8f\u5904\u7406\u6570\u636e\u3002<\/p>\n<p>list.stream()<br \/>\n    .map(this::compute)<br \/>\n    .forEach(System.out::println);<\/p>\n<p>\u4e32\u884c\u6d41\u7684\u7279\u70b9\u662f&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6267\u884c\u987a\u5e8f\u660e\u786e<\/p>\n<\/li>\n<li>\n<p>\u884c\u4e3a\u53ef\u9884\u6d4b<\/p>\n<\/li>\n<li>\n<p>\u7ebf\u7a0b\u5b89\u5168\u98ce\u9669\u4f4e<\/p>\n<\/li>\n<\/ul>\n<p>\u56e0\u6b64&#xff0c;\u5927\u591a\u6570\u4e1a\u52a1\u573a\u666f\u4e0b&#xff0c;\u4e32\u884c\u6d41\u662f\u9996\u9009\u65b9\u6848\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u4e32\u884c\u6d41\u867d\u7136\u4ee5\u5355\u7ebf\u7a0b\u65b9\u5f0f\u6267\u884c&#xff0c;\u4f46\u5176\u5e95\u5c42\u4ecd\u7136\u9075\u5faa Stream API \u7edf\u4e00\u7684\u6d41\u6c34\u7ebf&#xff08;Pipeline&#xff09;\u6267\u884c\u6a21\u578b\u3002 \u5728\u4e32\u884c\u6d41\u4e2d&#xff0c;\u7ec8\u6b62\u64cd\u4f5c\u4f5c\u4e3a\u6d88\u8d39\u7aef&#xff0c;\u6309\u9700\u4ece\u6570\u636e\u6e90\u4e2d\u9010\u4e2a\u62c9\u53d6\u5143\u7d20&#xff0c;\u6bcf\u4e2a\u5143\u7d20\u4f9d\u6b21\u7ecf\u8fc7\u6d41\u6c34\u7ebf\u4e2d\u7684\u5404\u4e2a\u4e2d\u95f4\u64cd\u4f5c\u8282\u70b9\u5b8c\u6210\u5904\u7406\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u4e32\u884c\u6d41\u5e76\u4e0d\u662f\u5bf9\u6bcf\u4e00\u4e2a\u4e2d\u95f4\u64cd\u4f5c\u5355\u72ec\u8fdb\u884c\u4e00\u6b21\u5b8c\u6574\u904d\u5386&#xff0c;\u800c\u662f\u91c7\u7528 \u9010\u5143\u7d20\u3001\u6309\u64cd\u4f5c\u94fe\u987a\u5e8f\u6267\u884c \u7684\u65b9\u5f0f\u5b8c\u6210\u8ba1\u7b97\u3002\u8fd9\u4e00\u6267\u884c\u6a21\u578b\u4e0e\u5e76\u884c\u6d41\u5728\u903b\u8f91\u7ed3\u6784\u4e0a\u4fdd\u6301\u4e00\u81f4&#xff0c;\u5dee\u5f02\u4ec5\u4f53\u73b0\u5728\u6267\u884c\u9a71\u52a8\u65b9\u5f0f\u4e0a\u3002<\/p>\n<p>\u5728\u4e32\u884c\u6d41\u573a\u666f\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7531\u5355\u4e00\u7ebf\u7a0b\u9a71\u52a8\u6574\u6761\u6d41\u6c34\u7ebf\u6267\u884c<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\u5143\u7d20\u6309\u7167\u6570\u636e\u6e90\u7684\u904d\u5386\u987a\u5e8f\u88ab\u4f9d\u6b21\u5904\u7406<\/p>\n<\/li>\n<li>\n<p>\u6bcf\u4e00\u65f6\u523b\u4ec5\u6709\u4e00\u4e2a\u5143\u7d20\u5728\u6d41\u6c34\u7ebf\u4e2d\u6d41\u8f6c<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd9\u79cd\u8bbe\u8ba1\u4fdd\u8bc1\u4e86\u4e32\u884c\u6d41\u5728\u5177\u5907\u826f\u597d\u53ef\u8bfb\u6027\u7684\u540c\u65f6&#xff0c;\u4ecd\u7136\u80fd\u591f\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u4e2d\u95f4\u96c6\u5408\u521b\u5efa&#xff0c;\u5e76\u7ef4\u6301\u8f83\u9ad8\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<h4 id=\"%EF%BC%882%EF%BC%89%E5%B9%B6%E8%A1%8C%E6%B5%81%EF%BC%88Parallel%20Stream%EF%BC%89\" style=\"margin-left:0px;margin-right:0px\">2.4.2\u00a0\u5e76\u884c\u6d41&#xff08;Parallel Stream&#xff09;<\/h4>\n<p style=\"margin-left:0;margin-right:0\">\u5e76\u884c\u6d41\u53ef\u4ee5\u901a\u8fc7 parallelStream() \u6216 stream().parallel() \u542f\u7528&#xff0c;\u5176\u5e95\u5c42\u5e76\u4e0d\u662f\u7b80\u5355\u5730\u201c\u5f00\u51e0\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8dd1\u201d&#xff0c;\u800c\u662f\u57fa\u4e8e Fork\/Join \u6846\u67b6 \u5b9e\u73b0\u7684\u4e00\u79cd\u81ea\u52a8\u5e76\u884c\u8ba1\u7b97\u6a21\u578b\u3002<\/p>\n<p>list.parallelStream()<br \/>\n    .map(this::heavyCompute)<br \/>\n    .forEach(System.out::println);<\/p>\n<p>\u5f53\u4f7f\u7528\u5e76\u884c\u6d41\u65f6&#xff0c;Stream \u4f1a\u5c1d\u8bd5\u5c06\u6570\u636e\u62c6\u5206\u4e3a\u591a\u4e2a\u5b50\u4efb\u52a1&#xff0c;\u5e76\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e2d\u5e76\u53d1\u6267\u884c&#xff0c;\u4ee5\u63d0\u5347 CPU \u5bc6\u96c6\u578b\u8ba1\u7b97\u573a\u666f\u4e0b\u7684\u6574\u4f53\u541e\u5410\u80fd\u529b\u3002<\/p>\n<h5 id=\"a.%E4%BB%80%E4%B9%88%E6%98%AF%20Fork%2FJoin%20%E6%A1%86%E6%9E%B6%EF%BC%9F\" style=\"margin-left:0px;margin-right:0px\">a.\u4ec0\u4e48\u662f Fork\/Join \u6846\u67b6&#xff1f;<\/h5>\n<p style=\"margin-left:0;margin-right:0\">Fork\/Join \u6846\u67b6\u662f Java 7 \u5f15\u5165\u7684\u4e00\u79cd\u5e76\u884c\u8ba1\u7b97\u6846\u67b6&#xff0c;\u4e3b\u8981\u7528\u4e8e\u89e3\u51b3\u53ef\u4ee5\u9012\u5f52\u62c6\u5206\u7684\u5927\u89c4\u6a21\u4efb\u52a1\u3002\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u53ef\u4ee5\u6982\u62ec\u4e3a\u4e24\u70b9&#xff1a;<\/p>\n<ul>\n<li>Fork&#xff08;\u62c6\u5206&#xff09;&#xff1a;\u5c06\u4e00\u4e2a\u5927\u4efb\u52a1\u62c6\u5206\u4e3a\u591a\u4e2a\u89c4\u6a21\u66f4\u5c0f\u7684\u5b50\u4efb\u52a1<\/li>\n<li>Join&#xff08;\u5408\u5e76&#xff09;&#xff1a;\u5728\u5b50\u4efb\u52a1\u6267\u884c\u5b8c\u6210\u540e&#xff0c;\u5c06\u7ed3\u679c\u5408\u5e76\u4e3a\u6700\u7ec8\u7ed3\u679c<\/li>\n<\/ul>\n<p style=\"margin-left:0;margin-right:0\">\u4e0e\u4f20\u7edf\u7ebf\u7a0b\u6c60\u4e0d\u540c&#xff0c;Fork\/Join \u6846\u67b6\u91c7\u7528\u4e86\u5de5\u4f5c\u7a83\u53d6&#xff08;Work-Stealing&#xff09;\u7b97\u6cd5\u3002\u6bcf\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\u90fd\u7ef4\u62a4\u4e00\u4e2a\u53cc\u7aef\u961f\u5217&#xff0c;\u5f53\u67d0\u4e2a\u7ebf\u7a0b\u5b8c\u6210\u81ea\u8eab\u4efb\u52a1\u540e&#xff0c;\u4f1a\u4e3b\u52a8\u201c\u7a83\u53d6\u201d\u5176\u4ed6\u7ebf\u7a0b\u5c1a\u672a\u5b8c\u6210\u7684\u4efb\u52a1&#xff0c;\u4ece\u800c\u63d0\u5347 CPU \u8d44\u6e90\u5229\u7528\u7387\u3002<\/p>\n<h5 id=\"b.%E5%B9%B6%E8%A1%8C%E6%B5%81%E7%9A%84%E6%89%A7%E8%A1%8C%E6%B5%81%E7%A8%8B\" style=\"margin-left:0px;margin-right:0px\">b.\u5e76\u884c\u6d41\u7684\u6267\u884c\u6d41\u7a0b<\/h5>\n<p style=\"margin-left:0;margin-right:0\">\u5728\u5e95\u5c42\u6267\u884c\u8fc7\u7a0b\u4e2d&#xff0c;\u5e76\u884c\u6d41\u901a\u5e38\u4f1a\u7ecf\u5386\u4ee5\u4e0b\u51e0\u4e2a\u9636\u6bb5&#xff1a;<\/p>\n<p style=\"margin-left:0;margin-right:0\">1\ufe0f\u20e3 \u4f7f\u7528 Spliterator \u62c6\u5206\u6570\u636e \u6570\u636e\u6e90\u5bf9\u5e94\u7684 Spliterator \u4f1a\u6839\u636e\u6570\u636e\u7ed3\u6784\u7279\u6027&#xff0c;\u5c06\u539f\u59cb\u6570\u636e\u4e0d\u65ad\u62c6\u5206\u4e3a\u66f4\u5c0f\u7684\u533a\u95f4&#xff0c;\u4ee5\u4fbf\u540e\u7eed\u5e76\u884c\u5904\u7406\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0\">Spliterator \u662f\u5bf9\u4f20\u7edf Iterator \u7684\u589e\u5f3a&#xff0c;\u7528\u4e8e\u652f\u6301 Stream \u5c24\u5176\u662f\u5e76\u884c\u6d41\u7684\u6570\u636e\u62c6\u5206\u4e0e\u904d\u5386\u3002\u4e0e\u53ea\u80fd\u987a\u5e8f\u904d\u5386\u7684\u8fed\u4ee3\u5668\u4e0d\u540c&#xff0c;Spliterator \u63d0\u4f9b\u4e86 \u6309\u9700\u62c6\u5206\u6570\u636e\u6e90\u7684\u80fd\u529b&#xff0c;\u8fd9\u662f\u5e76\u884c\u6d41\u5b9e\u73b0\u7684\u57fa\u7840\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0\">\u5176\u6838\u5fc3\u80fd\u529b\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>\u987a\u5e8f\u63a8\u8fdb\u5143\u7d20&#xff08;\u7c7b\u4f3c Iterator&#xff09;<\/li>\n<li>\u901a\u8fc7\u62c6\u5206\u64cd\u4f5c\u5c06\u6570\u636e\u6e90\u5212\u5206\u4e3a\u591a\u4e2a\u5b50\u533a\u95f4&#xff0c;\u4ee5\u652f\u6301\u5e76\u884c\u5904\u7406<\/li>\n<\/ul>\n<p style=\"margin-left:0;margin-right:0\">\u5728\u5e76\u884c\u6d41\u573a\u666f\u4e0b&#xff0c;Spliterator \u4f1a\u6839\u636e\u6570\u636e\u7ed3\u6784\u7279\u6027\u9012\u5f52\u62c6\u5206\u6570\u636e&#xff0c;\u4ece\u800c\u4e3a\u540e\u7eed\u591a\u7ebf\u7a0b\u6267\u884c\u63d0\u4f9b\u4efb\u52a1\u5212\u5206\u4f9d\u636e\u3002\u5bf9\u4e8e\u652f\u6301\u9ad8\u6548\u968f\u673a\u8bbf\u95ee\u7684\u6570\u636e\u7ed3\u6784&#xff08;\u5982 ArrayList&#xff09;&#xff0c;\u62c6\u5206\u6210\u672c\u8f83\u4f4e&#xff0c;\u5e76\u884c\u6267\u884c\u6548\u679c\u901a\u5e38\u66f4\u597d\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0\">2\ufe0f\u20e3 \u5c06\u5b50\u4efb\u52a1\u63d0\u4ea4\u5230 ForkJoinPool.commonPool \u62c6\u5206\u540e\u7684\u4efb\u52a1\u4f1a\u88ab\u63d0\u4ea4\u5230 JVM \u63d0\u4f9b\u7684\u516c\u5171 Fork\/Join \u7ebf\u7a0b\u6c60\u4e2d&#xff0c;\u7531\u591a\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\u5e76\u53d1\u6267\u884c\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0\">3\ufe0f\u20e3 \u591a\u4e2a\u7ebf\u7a0b\u5e76\u884c\u5904\u7406\u5b50\u4efb\u52a1 \u6bcf\u4e2a\u7ebf\u7a0b\u72ec\u7acb\u5904\u7406\u4e00\u90e8\u5206\u6570\u636e&#xff0c;\u6267\u884c\u76f8\u540c\u7684 Stream \u64cd\u4f5c\u94fe&#xff08;\u5982 map\u3001filter \u7b49&#xff09;\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0\">4\ufe0f\u20e3 \u5408\u5e76\u8ba1\u7b97\u7ed3\u679c&#xff08;\u5982\u6709\u9700\u8981&#xff09; \u5bf9\u4e8e reduce\u3001collect \u7b49\u7ec8\u6b62\u64cd\u4f5c&#xff0c;\u6846\u67b6\u4f1a\u5728\u5b50\u4efb\u52a1\u5b8c\u6210\u540e\u81ea\u52a8\u5408\u5e76\u5404\u4e2a\u7ebf\u7a0b\u7684\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n<h5 id=\"c.Fork%2FJoin%20%E4%B8%8E%E5%B9%B6%E8%A1%8C%E6%B5%81%E7%9A%84%E5%85%B3%E7%B3%BB\" style=\"margin-left:0px;margin-right:0px\">c.Fork\/Join \u4e0e\u5e76\u884c\u6d41\u7684\u5173\u7cfb<\/h5>\n<p style=\"margin-left:0;margin-right:0\">\u5e76\u884c\u6d41\u6b63\u662f\u5efa\u7acb\u5728 Fork\/Join \u6846\u67b6\u4e4b\u4e0a\u7684\u4e00\u79cd\u9ad8\u5c42\u62bd\u8c61&#xff1a;<\/p>\n<ul>\n<li>Stream \u8d1f\u8d23 \u5b9a\u4e49\u8ba1\u7b97\u89c4\u5219&#xff08;\u505a\u4ec0\u4e48&#xff09;<\/li>\n<li>Fork\/Join \u8d1f\u8d23 \u8c03\u5ea6\u7ebf\u7a0b\u548c\u6267\u884c\u4efb\u52a1&#xff08;\u600e\u4e48\u5e76\u884c&#xff09;<\/li>\n<\/ul>\n<p style=\"margin-left:0;margin-right:0\">\u5e76\u884c\u6d41\u9ed8\u8ba4\u4f7f\u7528\u7684\u662f&#xff1a;<\/p>\n<p>ForkJoinPool.commonPool<\/p>\n<p style=\"margin-left:0;margin-right:0\">\u8be5\u7ebf\u7a0b\u6c60\u5177\u6709\u4ee5\u4e0b\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u7ebf\u7a0b\u6570\u901a\u5e38\u4e3a CPU \u6838\u5fc3\u6570 &#8211; 1<\/li>\n<li>JVM \u8fdb\u7a0b\u5185\u5168\u5c40\u5171\u4eab<\/li>\n<li>\u4f1a\u4e0e\u5176\u4ed6\u5e76\u884c\u4efb\u52a1&#xff08;\u5982 CompletableFuture&#xff09;\u7ade\u4e89\u7ebf\u7a0b\u8d44\u6e90<\/li>\n<\/ul>\n<hr \/>\n<h2 id=\"3.%20Optional%EF%BC%9A%E6%98%BE%E5%BC%8F%E8%A1%A8%E8%BE%BE%E2%80%9C%E5%80%BC%E5%8F%AF%E8%83%BD%E4%B8%8D%E5%AD%98%E5%9C%A8%E2%80%9D%E7%9A%84%E8%AF%AD%E4%B9%89\" style=\"margin-left:0px;margin-right:0px\">3. Optional&#xff1a;\u663e\u5f0f\u8868\u8fbe\u201c\u503c\u53ef\u80fd\u4e0d\u5b58\u5728\u201d\u7684\u8bed\u4e49<\/h2>\n<p style=\"margin-left:0;margin-right:0\">\u5728 Java 8 \u4e4b\u524d&#xff0c;null \u662f\u5bf9\u8c61\u7f3a\u5931\u7684\u4e3b\u8981\u8868\u8fbe\u65b9\u5f0f&#xff0c;\u4f46\u9690\u5f0f\u7684\u7a7a\u503c\u8bed\u4e49\u6781\u6613\u5f15\u53d1\u7a7a\u6307\u9488\u5f02\u5e38&#xff0c;\u5c24\u5176\u5728\u591a\u5c42\u5bf9\u8c61\u8bbf\u95ee\u6216\u94fe\u5f0f\u8c03\u7528\u573a\u666f\u4e2d\u3002Optional \u7684\u5f15\u5165&#xff0c;\u4e3a\u201c\u503c\u53ef\u80fd\u4e0d\u5b58\u5728\u201d\u63d0\u4f9b\u4e86\u4e00\u79cd\u663e\u5f0f\u4e14\u53d7\u7ea6\u675f\u7684\u8868\u8fbe\u65b9\u5f0f\u3002<\/p>\n<p style=\"margin-left:0;margin-right:0\">Optional \u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u6700\u591a\u53ea\u5305\u542b\u4e00\u4e2a\u503c\u7684\u5bb9\u5668&#xff0c;\u7528\u4e8e\u63d0\u9192\u8c03\u7528\u8005\u5728\u4f7f\u7528\u503c\u4e4b\u524d\u5fc5\u987b\u8003\u8651\u5176\u5b58\u5728\u6027\u3002<\/p>\n<p>\/\/ \u4f20\u7edf\u5224\u7a7a\u5199\u6cd5<br \/>\nif (user !&#061; null &amp;&amp; user.getName() !&#061; null) {<br \/>\n    System.out.println(user.getName());<br \/>\n}<\/p>\n<p>\/\/ Optional \u5199\u6cd5<br \/>\nOptional.ofNullable(user)<br \/>\n        .map(User::getName)<br \/>\n        .ifPresent(System.out::println);<\/p>\n<p>\u901a\u8fc7 Optional&#xff0c;\u7a7a\u503c\u5904\u7406\u903b\u8f91\u53ef\u4ee5\u4e0e\u4e1a\u52a1\u903b\u8f91\u81ea\u7136\u7ec4\u5408&#xff0c;\u4f7f\u5224\u7a7a\u884c\u4e3a\u66f4\u52a0\u96c6\u4e2d\u548c\u7edf\u4e00&#xff0c;\u4ece\u800c\u63d0\u5347\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55<br \/>\n1. Lambda \u8868\u8fbe\u5f0f&#xff1a;\u5c06\u201c\u884c\u4e3a\u903b\u8f91\u201d\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012<br \/>\n2. Stream API&#xff1a;\u57fa\u4e8e\u6d41\u6c34\u7ebf\u6a21\u578b\u7684\u6570\u636e\u5904\u7406\u673a\u5236&#xff08;\u8be6\u89e3&#xff09;<br \/>\n2.1 Stream \u7684\u6838\u5fc3\u601d\u60f3&#xff1a;\u63cf\u8ff0\u201c\u6570\u636e\u5982\u4f55\u88ab\u5904\u7406\u201d<br \/>\n2.2 Stream \u7684\u4e09\u5927\u7ec4\u6210\u90e8\u5206<br \/>\n2.2.1 \u6570\u636e\u6e90&#xff08;Source&#xff09;<br \/>\n2.2.2 \u4e2d\u95f4\u64cd\u4f5c&#xff08;Intermediate Operation&#xff09;<br \/>\n2.2.3<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,123,190],"topic":[],"class_list":["post-73280","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-123","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java 8 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5165\u95e8\uff1a\u4ece Lambda \u5230 Stream \u5e76\u884c\u6267\u884c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/73280.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java 8 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5165\u95e8\uff1a\u4ece Lambda \u5230 Stream \u5e76\u884c\u6267\u884c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u76ee\u5f55 1. Lambda \u8868\u8fbe\u5f0f&#xff1a;\u5c06\u201c\u884c\u4e3a\u903b\u8f91\u201d\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012 2. Stream API&#xff1a;\u57fa\u4e8e\u6d41\u6c34\u7ebf\u6a21\u578b\u7684\u6570\u636e\u5904\u7406\u673a\u5236&#xff08;\u8be6\u89e3&#xff09; 2.1 Stream \u7684\u6838\u5fc3\u601d\u60f3&#xff1a;\u63cf\u8ff0\u201c\u6570\u636e\u5982\u4f55\u88ab\u5904\u7406\u201d 2.2 Stream \u7684\u4e09\u5927\u7ec4\u6210\u90e8\u5206 2.2.1 \u6570\u636e\u6e90&#xff08;Source&#xff09; 2.2.2 \u4e2d\u95f4\u64cd\u4f5c&#xff08;Intermediate Operation&#xff09; 2.2.3\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/73280.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-07T03:26:01+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/73280.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/73280.html\",\"name\":\"Java 8 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5165\u95e8\uff1a\u4ece Lambda \u5230 Stream \u5e76\u884c\u6267\u884c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-07T03:26:01+00:00\",\"dateModified\":\"2026-02-07T03:26:01+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/73280.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/73280.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/73280.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java 8 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5165\u95e8\uff1a\u4ece Lambda \u5230 Stream \u5e76\u884c\u6267\u884c\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java 8 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5165\u95e8\uff1a\u4ece Lambda \u5230 Stream \u5e76\u884c\u6267\u884c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/73280.html","og_locale":"zh_CN","og_type":"article","og_title":"Java 8 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5165\u95e8\uff1a\u4ece Lambda \u5230 Stream \u5e76\u884c\u6267\u884c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u76ee\u5f55 1. Lambda \u8868\u8fbe\u5f0f&#xff1a;\u5c06\u201c\u884c\u4e3a\u903b\u8f91\u201d\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012 2. Stream API&#xff1a;\u57fa\u4e8e\u6d41\u6c34\u7ebf\u6a21\u578b\u7684\u6570\u636e\u5904\u7406\u673a\u5236&#xff08;\u8be6\u89e3&#xff09; 2.1 Stream \u7684\u6838\u5fc3\u601d\u60f3&#xff1a;\u63cf\u8ff0\u201c\u6570\u636e\u5982\u4f55\u88ab\u5904\u7406\u201d 2.2 Stream \u7684\u4e09\u5927\u7ec4\u6210\u90e8\u5206 2.2.1 \u6570\u636e\u6e90&#xff08;Source&#xff09; 2.2.2 \u4e2d\u95f4\u64cd\u4f5c&#xff08;Intermediate Operation&#xff09; 2.2.3","og_url":"https:\/\/www.wsisp.com\/helps\/73280.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-07T03:26:01+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/73280.html","url":"https:\/\/www.wsisp.com\/helps\/73280.html","name":"Java 8 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5165\u95e8\uff1a\u4ece Lambda \u5230 Stream \u5e76\u884c\u6267\u884c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-07T03:26:01+00:00","dateModified":"2026-02-07T03:26:01+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/73280.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/73280.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/73280.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Java 8 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5165\u95e8\uff1a\u4ece Lambda \u5230 Stream \u5e76\u884c\u6267\u884c"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/73280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=73280"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/73280\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=73280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=73280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=73280"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=73280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}