{"id":36888,"date":"2024-11-26T13:47:38","date_gmt":"2024-11-26T05:47:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36888\/"},"modified":"2024-11-26T13:47:38","modified_gmt":"2024-11-26T05:47:38","slug":"java%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e7%94%a8stream","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36888\/","title":{"rendered":"java\u6570\u7ec4\u600e\u4e48\u7528stream"},"content":{"rendered":"<blockquote><p>\n  java \u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8f6c\u6362\u4e3a stream\uff1a1. \u521b\u5efa\u6570\u7ec4\u30022. \u4f7f\u7528 arrays.stream(\u6570\u7ec4) \u8f6c\u6362\u6570\u7ec4\u4e3a stream\u30023. \u4f7f\u7528 stream \u8fdb\u884c\u64cd\u4f5c\uff0c\u4f8b\u5982\u8fc7\u6ee4\u3001\u6620\u5c04\u548c\u5f52\u7ea6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111221002076546.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u600e\u4e48\u7528stream\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u600e\u4e48\u7528stream\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u5982\u4f55\u4f7f\u7528 Stream<\/strong><\/p>\n<p>Stream \u662f Java 8 \u4e2d\u5f15\u5165\u7684\u4e00\u4e2a\u5f3a\u5927\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u5bf9\u6570\u636e\u96c6\u5408\u8fdb\u884c\u58f0\u660e\u5f0f\u64cd\u4f5c\u3002Java \u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8f6c\u6362\u4e3a Stream\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u6570\u7ec4<\/strong><\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u8f6c\u6362\u6570\u7ec4\u4e3a Stream<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>IntStream stream = Arrays.stream(numbers);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f6c\u6362\u540e\uff0cstream \u5c31\u5305\u542b\u4e86\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<p><strong>3. \u4f7f\u7528 Stream \u8fdb\u884c\u64cd\u4f5c<\/strong><\/p>\n<p>Stream \u63d0\u4f9b\u4e86\u5404\u79cd\u64cd\u4f5c\uff0c\u5305\u62ec\u8fc7\u6ee4\u3001\u6620\u5c04\u548c\u5f52\u7ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>\/\/ \u8fc7\u6ee4\u5076\u6570\nIntStream evenNumbers = stream.filter(n -&gt; n % 2 == 0);\n\n\/\/ \u6620\u5c04\u6bcf\u4e2a\u5143\u7d20\uff0c\u4f7f\u5176\u4e58\u4ee5 2\nIntStream doubledNumbers = stream.map(n -&gt; n * 2);\n\n\/\/ \u5f52\u7ea6\u6240\u6709\u5143\u7d20\uff0c\u8ba1\u7b97\u603b\u548c\nint sum = stream.sum();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 Stream \u5bf9\u6570\u7ec4\u8fdb\u884c\u4ee5\u4e0b\u64cd\u4f5c\u7684\u793a\u4f8b\uff1a<\/p>\n<ul>\n<li>\u8fc7\u6ee4\u6570\u5b57\uff0c\u53ea\u4fdd\u7559\u5947\u6570<\/li>\n<li>\u5c06\u5947\u6570\u6620\u5c04\u4e3a\u5b83\u4eec\u7684\u5e73\u65b9<\/li>\n<li>\u8ba1\u7b97\u5e73\u65b9\u548c<\/li>\n<\/ul>\n<pre>int[] numbers = {1, 2, 3, 4, 5};\n\nIntStream stream = Arrays.stream(numbers);\n\nint sumOfSquares = stream\n    .filter(n -&gt; n % 2 != 0)\n    .map(n -&gt; n * n)\n    .sum();\n\nSystem.out.println(sumOfSquares); \/\/ \u8f93\u51fa\uff1a35<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u7528stream\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8f6c\u6362\u4e3a stream\uff1a1. \u521b\u5efa\u6570\u7ec4\u30022. \u4f7f\u7528 arrays.stream(\u6570\u7ec4) \u8f6c\u6362\u6570\u7ec4\u4e3a stream\u30023. \u4f7f\u7528 stream \u8fdb\u884c\u64cd\u4f5c\uff0c\u4f8b\u5982\u8fc7\u6ee4\u3001\u6620\u5c04\u548c\u5f52\u7ea6\u3002 Java \u6570\u7ec4\u5982\u4f55\u4f7f\u7528 Stream Stream \u662f Java 8 \u4e2d\u5f15\u5165\u7684\u4e00\u4e2a\u5f3a\u5927\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u5bf9\u6570\u636e\u96c6\u5408\u8fdb\u884c\u58f0\u660e\u5f0f\u64cd\u4f5c\u3002Java \u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8f6c\u6362\u4e3a Stream\uff1a 1. \u521b\u5efa\u6570\u7ec4 int[] numbers = {1, 2, 3, 4, 5}; \u767b\u5f55\u540e\u590d\u5236 2. \u8f6c\u6362\u6570\u7ec4\u4e3a Stream \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b IntStream stream = Arrays.stream(numbers); \u767b\u5f55\u540e\u590d\u5236 \u8f6c\u6362\u540e\uff0cstream \u5c31\u5305\u542b\u4e86\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002 3. \u4f7f\u7528 Stream \u8fdb\u884c\u64cd\u4f5c Stream \u63d0\u4f9b\u4e86\u5404\u79cd\u64cd\u4f5c\uff0c\u5305\u62ec\u8fc7\u6ee4\u3001\u6620\u5c04\u548c\u5f52\u7ea6\u3002\u4f8b\u5982\uff1a \/\/ \u8fc7\u6ee4\u5076\u6570 IntStream evenNumbers = stream.filter(n [&hellip;]<\/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-36888","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36888","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=36888"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36888\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}