

{"id":960,"date":"2021-02-22T12:37:51","date_gmt":"2021-02-22T04:37:51","guid":{"rendered":"https:\/\/www.52dixiaowo.com\/java\/?p=960"},"modified":"2021-02-22T12:37:52","modified_gmt":"2021-02-22T04:37:52","slug":"stream-%e6%b5%81%e5%bc%8f%e6%8e%a5%e5%8f%a3%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.52dixiaowo.com\/java\/post-960.html","title":{"rendered":"Stream &#8212; \u6d41\u5f0f\u63a5\u53e3\u7c7b"},"content":{"rendered":"\n<p>Stream \u63a5\u53e3\u7c7b\u662f\u8fdb\u884c Stream \u64cd\u4f5c\u7684\u5fc5\u987b\u7684\u7c7b<\/p>\n\n\n\n<h5>1.\u5bfc\u5305<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.stream.Stream;<\/code><\/pre>\n\n\n\n<h5>2.\u6784\u9020\u65b9\u6cd5<\/h5>\n\n\n\n<p>Stream&lt;T> \u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u65e0\u6cd5\u76f4\u63a5\u83b7\u53d6\uff0c\u4e3b\u8981\u6709 2 \u79cd\u65b9\u5f0f\u83b7\u53d6\uff0c\u96c6\u5408\u548c\u6570\u7ec4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/1.\u6240\u6709\u7684 Collection \u96c6\u5408\u90fd\u53ef\u4ee5\u901a\u8fc7 stream \u9ed8\u8ba4\u65b9\u6cd5\u83b7\u53d6\nCollection&lt;String> collection = new ArrayList&lt;>();\nStream&lt;String> stream1 = collection.stream();\n\/\/2.Map\u96c6\u5408\u53ef\u4ee5\u901a\u8fc7 keySet\uff0c\u6216 entrySet\uff0c\u518d\u83b7\u53d6stream\nMap&lt;String,String> map = new HashMap&lt;>();\nSet&lt;String> keyset = map.KeySet();\nStream&lt;String> stream2 = keyset.stream();\nSet&lt;Map.Entry&lt;String,String>> entry = map.entrySet();\nStream&lt;Map.Entry&lt;String,String>> stream3 = entry.stream;\n\/\/3.\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7 Stream \u63a5\u53e3\u7684\u9759\u6001\u65b9\u6cd5 of \u83b7\u53d6\nInteger&#91;] arr = {1,2,3,4,5,6};\nStream&lt;Integer> stream4 = Stream.of(arr);<\/code><\/pre>\n\n\n\n<h5>3.\u65b9\u6cd5<\/h5>\n\n\n\n<p>\u65b9\u6cd5\u4e3b\u8981\u5206\u4e3a 2 \u5927\u7c7b\uff0c\u5206\u4e3a\u5ef6\u8fdf\u65b9\u6cd5 (\u8fd4\u56de\u4ecd\u662fStream) \u548c\u7ec8\u7ed3\u65b9\u6cd5<\/p>\n\n\n\n<p>\u7ec8\u7ed3\u65b9\u6cd5\u5305\u62ec count \u548c forEach\uff0c\u5176\u4f59\u65b9\u6cd5\u5747\u4e3a\u5ef6\u8fdf\u65b9\u6cd5<\/p>\n\n\n\n<p>\u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u5747\u662f\u4e00\u4e2a\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c\u6bcf\u4e2a Stream \u53ea\u80fd\u7528\u4e00\u6b21\u65b9\u6cd5\uff0c\u53ef\u94fe\u5f0f\u8c03\u7528<\/p>\n\n\n\n<ol><li>forEach \u65b9\u6cd5\uff0c\u4e3b\u8981\u662f\u7528\u6765\u904d\u5386\u96c6\u5408<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>Stream&lt;String> stream = Stream.of(\"\u5f20\u4e09\",\"\u674e\u56db\",\"\u738b\u4e94\");\nstream.forEach(name->System.out.println(name));<\/code><\/pre>\n\n\n\n<p>2. filter \u65b9\u6cd5\uff0c\u62e6\u622a\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Stream&lt;String> stream2 = Stream.of(\"\u5f20\u4e09\u4e30\",\"\u5f20\u65e0\u5fcc\",\"\u5f20\u4e09\",\"\u674e\u56db\");\nStream&lt;String> stream3 = stream2.filter(name->name.startWith(\"\u5f20\"));<\/code><\/pre>\n\n\n\n<p>3. map\u65b9\u6cd5\uff0c\u8f6c\u6362\u6570\u636e\u7c7b\u578b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Stream&lt;String> stream4 = Stream.of(\"1\",\"2\",\"3\");\nStream&lt;Integer> stream5 = stream4.map(s->Integer.parseInt(s));<\/code><\/pre>\n\n\n\n<p>4. count\u65b9\u6cd5\uff0c\u8ba1\u7b97 Stream \u6d41\u4e2d\u5143\u7d20\u7684\u4e2a\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Stream&lt;String> stream6 = Stream.of(\"\u5f20\u4e09\",\"\u674e\u56db\");\nlong count = stream6.count();<\/code><\/pre>\n\n\n\n<p>5. limit\u65b9\u6cd5\uff0c\u622a\u53d6\u524dn\u4e2a\u5143\u7d20<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Stream&lt;String> stream7 = Stream.of(\"\u7b2c\u4e00\u4e2a\",\"\u7b2c\u4e8c\u4e2a\",\"\u7b2c\u4e09\u4e2a\");\nString&lt;String> stream8 = stream7.limit(2); \/\/\u53ea\u8981\u524d 2 \u4e2a\u5143\u7d20<\/code><\/pre>\n\n\n\n<p>6. skip\u65b9\u6cd5\uff0c\u8df3\u8fc7\u524dn\u4e2a\u5143\u7d20<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Stream&lt;String> stream9 = Stream.of(\"1\",\"2\",\"3\",\"4\",\"5\");\nStream&lt;String> stream10 = stream9.skip(3); \/\/\u8df3\u8fc7\u524d3\u4e2a\uff0c\u53ea\u67094,5<\/code><\/pre>\n\n\n\n<p>7. concat\u65b9\u6cd5\uff0c\u628a2\u4e2a\u6d41\u5408\u5e76\u4e3a\u4e00\u4e2a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Stream&lt;String> stream11 = Stream.of(\"1\",\"2\");\nStream&lt;String> stream12 = Stream.of(\"3\",\"4\");\nStream&lt;String> stream13 = Stream.concat(stream11,stream12);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Stream \u63a5\u53e3\u7c7b\u662f\u8fdb\u884c Stream \u64cd\u4f5c\u7684\u5fc5\u987b\u7684\u7c7b 1.\u5bfc\u5305 2.\u6784\u9020\u65b9\u6cd5 Stream&lt;T> \u662f&hellip; <a href=\"https:\/\/www.52dixiaowo.com\/java\/post-960.html\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">Stream &#8212; \u6d41\u5f0f\u63a5\u53e3\u7c7b<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29],"tags":[],"_links":{"self":[{"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/posts\/960"}],"collection":[{"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/comments?post=960"}],"version-history":[{"count":0,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/posts\/960\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/media?parent=960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/categories?post=960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/tags?post=960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}