{"id":64847,"date":"2025-05-03T11:26:35","date_gmt":"2025-05-03T03:26:35","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64847\/"},"modified":"2025-05-03T11:26:35","modified_gmt":"2025-05-03T03:26:35","slug":"java-stream-%e6%b3%9b%e5%9e%8b%e6%9c%aa%e5%a1%ab%e5%86%99%e4%b8%ba%e4%bd%95%e4%bc%9a%e5%af%bc%e8%87%b4%e7%b1%bb%e5%9e%8b%e6%93%a6%e9%99%a4%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64847\/","title":{"rendered":"Java Stream \u6cdb\u578b\u672a\u586b\u5199\u4e3a\u4f55\u4f1a\u5bfc\u81f4\u7c7b\u578b\u64e6\u9664\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173080658655902.jpg\" class=\"aligncenter\" title=\"Java Stream \u6cdb\u578b\u672a\u586b\u5199\u4e3a\u4f55\u4f1a\u5bfc\u81f4\u7c7b\u578b\u64e6\u9664\uff1f\u63d2\u56fe\" alt=\"Java Stream \u6cdb\u578b\u672a\u586b\u5199\u4e3a\u4f55\u4f1a\u5bfc\u81f4\u7c7b\u578b\u64e6\u9664\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>java stream \u6cdb\u578b\u672a\u586b\u5199\u5bfc\u81f4\u7c7b\u578b\u64e6\u9664\u7684\u6df1\u5c42\u539f\u56e0<\/strong><\/p>\n<p>\u5728java \u4e2d\uff0c\u5982\u679c\u4e00\u4e2a\u96c6\u5408\u672a\u6307\u5b9a\u6cdb\u578b\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u89c6\u4e3aobject \u7c7b\u578b\u96c6\u5408\u3002\u8fd9\u610f\u5473\u7740\u96c6\u5408\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u4f55\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5305\u62ec string\u3001integer \u7b49\u3002 <\/p>\n<p>\u5f53\u5bf9\u8fd9\u79cd\u672a\u6307\u5b9a\u6cdb\u578b\u96c6\u5408\u5e94\u7528 stream \u6d41\u64cd\u4f5c\u65f6\uff0c\u6d41\u7684\u4e2d\u4ecb\u64cd\u4f5c\uff08\u5982 map\u3001filter\uff09\u4f1a\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u7c7b\u578b\u64e6\u9664\u3002\u8fd9\u610f\u5473\u7740\u8fd9\u4e9b\u64cd\u4f5c\u5c06\u628a\u5143\u7d20\u89c6\u4e3a object \u7c7b\u578b\u5bf9\u8c61\uff0c\u5373\u4f7f\u5728\u539f\u59cb\u96c6\u5408\u4e2d\u5b83\u4eec\u662f\u7279\u5b9a\u7c7b\u578b\uff08\u5982 string\uff09\u3002 <\/p>\n<p>\u8fd9\u4f1a\u5bfc\u81f4 downstream \u7684\u64cd\u4f5c\u65e0\u6cd5\u51c6\u786e\u8bc6\u522b\u5143\u7d20\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5728\u8fc7\u6ee4\u64cd\u4f5c\u4e2d\uff0c\u65e0\u6cd5\u5224\u65ad\u5143\u7d20\u662f\u5426\u4e3a string \u7c7b\u578b\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u5df2\u7ecf\u5c06\u5143\u7d20\u7c7b\u578b\u64e6\u9664\u4e86\u3002 <\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u8fd9\u79cd\u73b0\u8c61\u7684\u6df1\u5c42\u539f\u56e0\u5728\u4e8e java \u662f\u4e00\u79cd\u7f16\u8bd1\u578b\u8bed\u8a00\uff0c\u5728\u8fd0\u884c\u65f6\u6ca1\u6709\u6cdb\u578b\u6982\u5ff5\u3002\u7f16\u8bd1\u5668\u5c06\u6cdb\u578b\u4fe1\u606f\u4f5c\u4e3a\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\uff0c\u800c\u5728\u8fd0\u884c\u65f6\u5c06\u6cdb\u578b\u7c7b\u578b\u64e6\u9664\u4e3a object \u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u5728\u8fd0\u884c\u65f6\u5f3a\u5236\u6267\u884c\u6cdb\u578b\u7ea6\u675f\uff0c\u4ece\u800c\u5bfc\u81f4\u7c7b\u578b\u64e6\u9664\u3002 <\/p>\n<p>\u4e3a\u4e86\u9632\u6b62\u7c7b\u578b\u64e6\u9664\u5e76\u786e\u4fdd\u6d41\u64cd\u4f5c\u80fd\u591f\u6b63\u786e\u6267\u884c\uff0c\u5fc5\u987b\u4e3a\u96c6\u5408\u663e\u5f0f\u6307\u5b9a\u6cdb\u578b\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>List&lt;String&gt; list = new ArrayList&lt;&gt;();\nlist.add(\"aaa\");\n\nlist.stream().map(a -&gt; a.toString())\n        .filter(a -&gt; a != null)  \/\/ \u6b64\u65f6 a \u7684\u7c7b\u578b\u4e3a String\uff0c\u53ef\u4ee5\u8fdb\u884c\u975e\u7a7a\u5224\u65ad<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava Stream \u6cdb\u578b\u672a\u586b\u5199\u4e3a\u4f55\u4f1a\u5bfc\u81f4\u7c7b\u578b\u64e6\u9664\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java stream \u6cdb\u578b\u672a\u586b\u5199\u5bfc\u81f4\u7c7b\u578b\u64e6\u9664\u7684\u6df1\u5c42\u539f\u56e0 \u5728java \u4e2d\uff0c\u5982\u679c\u4e00\u4e2a\u96c6\u5408\u672a\u6307\u5b9a\u6cdb\u578b\uff0c\u5219\u7f16\u8bd1\u5668\u4f1a\u5c06\u5176\u89c6\u4e3aobject \u7c7b\u578b\u96c6\u5408\u3002\u8fd9\u610f\u5473\u7740\u96c6\u5408\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u4f55\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5305\u62ec string\u3001integer \u7b49\u3002 \u5f53\u5bf9\u8fd9\u79cd\u672a\u6307\u5b9a\u6cdb\u578b\u96c6\u5408\u5e94\u7528 stream \u6d41\u64cd\u4f5c\u65f6\uff0c\u6d41\u7684\u4e2d\u4ecb\u64cd\u4f5c\uff08\u5982 map\u3001filter\uff09\u4f1a\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u7c7b\u578b\u64e6\u9664\u3002\u8fd9\u610f\u5473\u7740\u8fd9\u4e9b\u64cd\u4f5c\u5c06\u628a\u5143\u7d20\u89c6\u4e3a object \u7c7b\u578b\u5bf9\u8c61\uff0c\u5373\u4f7f\u5728\u539f\u59cb\u96c6\u5408\u4e2d\u5b83\u4eec\u662f\u7279\u5b9a\u7c7b\u578b\uff08\u5982 string\uff09\u3002 \u8fd9\u4f1a\u5bfc\u81f4 downstream \u7684\u64cd\u4f5c\u65e0\u6cd5\u51c6\u786e\u8bc6\u522b\u5143\u7d20\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5728\u8fc7\u6ee4\u64cd\u4f5c\u4e2d\uff0c\u65e0\u6cd5\u5224\u65ad\u5143\u7d20\u662f\u5426\u4e3a string \u7c7b\u578b\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u5df2\u7ecf\u5c06\u5143\u7d20\u7c7b\u578b\u64e6\u9664\u4e86\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u8fd9\u79cd\u73b0\u8c61\u7684\u6df1\u5c42\u539f\u56e0\u5728\u4e8e java \u662f\u4e00\u79cd\u7f16\u8bd1\u578b\u8bed\u8a00\uff0c\u5728\u8fd0\u884c\u65f6\u6ca1\u6709\u6cdb\u578b\u6982\u5ff5\u3002\u7f16\u8bd1\u5668\u5c06\u6cdb\u578b\u4fe1\u606f\u4f5c\u4e3a\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\uff0c\u800c\u5728\u8fd0\u884c\u65f6\u5c06\u6cdb\u578b\u7c7b\u578b\u64e6\u9664\u4e3a object \u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u5728\u8fd0\u884c\u65f6\u5f3a\u5236\u6267\u884c\u6cdb\u578b\u7ea6\u675f\uff0c\u4ece\u800c\u5bfc\u81f4\u7c7b\u578b\u64e6\u9664\u3002 \u4e3a\u4e86\u9632\u6b62\u7c7b\u578b\u64e6\u9664\u5e76\u786e\u4fdd\u6d41\u64cd\u4f5c\u80fd\u591f\u6b63\u786e\u6267\u884c\uff0c\u5fc5\u987b\u4e3a\u96c6\u5408\u663e\u5f0f\u6307\u5b9a\u6cdb\u578b\u7c7b\u578b\u3002\u4f8b\u5982\uff1a List&lt;String&gt; list = new ArrayList&lt;&gt;(); list.add(&#8220;aaa&#8221;); list.stream().map(a -&gt; a.toString()) .filter(a -&gt; a != null) \/\/ \u6b64\u65f6 a \u7684\u7c7b\u578b\u4e3a String\uff0c\u53ef\u4ee5\u8fdb\u884c\u975e\u7a7a\u5224\u65ad \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662fJava Stream \u6cdb\u578b\u672a\u586b\u5199\u4e3a\u4f55\u4f1a\u5bfc\u81f4\u7c7b\u578b\u64e6\u9664\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-64847","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=64847"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64847\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}