{"id":491,"date":"2024-11-08T19:59:20","date_gmt":"2024-11-08T11:59:20","guid":{"rendered":"http:\/\/www.cmd137blog.top\/?p=491"},"modified":"2024-11-08T19:59:23","modified_gmt":"2024-11-08T11:59:23","slug":"java-stream%e6%b5%81","status":"publish","type":"post","link":"http:\/\/www.cmd137blog.top\/?p=491","title":{"rendered":"Java-Stream\u6d41"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">1.\u6d41\u7684\u601d\u60f3\uff1a<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u96c6\u5408\u4e0e\u6d41\u7684\u8f6c\u6362<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u53ef\u4ee5\u5c06\u96c6\u5408\uff08\u5982<code>List<\/code>\u3001<code>Set<\/code>\uff09\u8f6c\u6362\u4e3a\u6d41\uff08<code>Stream<\/code>\uff09\uff0c\u4ee5\u4fbf\u8fdb\u884c\u6d41\u5f0f\u5904\u7406\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u4e2d\u95f4\u64cd\u4f5c\uff08Intermediate Operations\uff09<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u4e9b\u64cd\u4f5c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6d41\uff0c\u53ef\u4ee5\u8fdb\u884c\u94fe\u5f0f\u64cd\u4f5c\uff0c\u5982<code>filter<\/code>\uff08\u8fc7\u6ee4\uff09\u3001<code>map<\/code>\uff08\u8f6c\u6362\uff09\u3001<code>sorted<\/code>\uff08\u6392\u5e8f\uff09\u7b49\u3002<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">\u4e14\u5efa\u8bae\u4f7f\u7528\u94fe\u5f0f\u7f16\u7a0b<\/mark>\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u7ec8\u6b62\u64cd\u4f5c\uff08Terminal Operations\uff09<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u7ec8\u6b62\u64cd\u4f5c\u4f1a\u6d88\u8017\u6d41\uff0c\u5e76\u4ea7\u751f\u4e00\u4e2a\u6700\u7ec8\u7684\u7ed3\u679c\u6216\u526f\u4f5c\u7528\uff0c\u5982<code>forEach<\/code>\uff08\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u64cd\u4f5c\uff09\u3001<code>collect<\/code>\uff08\u5c06\u6d41\u8f6c\u6362\u4e3a\u5176\u4ed6\u5f62\u5f0f\uff0c\u5982\u96c6\u5408\uff09\u3001<code>reduce<\/code>\uff08\u805a\u5408\u64cd\u4f5c\uff09\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u4e0d\u53ef\u53d8\u6027<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>Stream\u64cd\u4f5c\u4e0d\u4f1a\u4fee\u6539\u539f\u59cb\u6570\u636e\u6e90\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6d41\u6216\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u60f0\u6027\u6c42\u503c<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u6d41\u64cd\u4f5c\u662f\u60f0\u6027\u6c42\u503c\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5728\u6267\u884c\u7ec8\u6b62\u64cd\u4f5c\u4e4b\u524d\uff0c\u4e2d\u95f4\u64cd\u4f5c\u4e0d\u4f1a\u6267\u884c\u3002\u8fd9\u79cd\u8bbe\u8ba1\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u8df3\u8fc7\u4e0d\u5fc5\u8981\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">2.\u6d41\u7684\u4f7f\u7528\u6b65\u9aa4\uff1a<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5148\u5f97\u5230\u4e00\u6761Stream\u6d41\uff0c\u5e76\u628a\u6570\u636e\u653e\u4e0a\u53bb\uff1b<\/li>\n\n\n\n<li>\u5229\u7528Stream\u6d41\u4e2d\u7684API\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\n<ul class=\"wp-block-list\">\n<li>\u4e2d\u95f4\u65b9\u6cd5\uff1a\u65b9\u6cd5\u8c03\u7528\u5b8c\u6bd5\u540e\uff0c\u8fd8\u53ef\u4ee5\u8c03\u7528\u5176\u4ed6\u65b9\u6cd5<\/li>\n\n\n\n<li>\u7ec8\u7ed3\u65b9\u6cd5\uff1a\u6700\u540e\u4e00\u6b65\uff0c\u8c03\u7528\u5b8c\u6bd5\u540e\uff0c\u4e0d\u80fd\u5728\u8c03\u7528\u5176\u4ed6\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">3.\u6d41\u7684\u83b7\u53d6\uff1a<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u83b7\u53d6\u65b9\u5f0f<\/th><th>\u65b9\u6cd5\u540d<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u5355\u5217\u96c6\u5408<\/td><td>default Stream stream()<\/td><td>Collection\u4e2d\u7684\u9ed8\u8ba4\u65b9\u6cd5<\/td><\/tr><tr><td>\u53cc\u5217\u96c6\u5408<\/td><td>\u65e0<\/td><td>\u65e0\u6cd5\u76f4\u63a5\u4f7f\u7528stream\u6d41<br>\u53ef\u4ee5\u5148\u8f6c\u6210\u5355\u5217\u96c6\u5408(keySet\u3001entrySet\u3001values)<\/td><\/tr><tr><td>\u6570\u7ec4<\/td><td>public static Stream stream(T[] array)<\/td><td>Arrays\u5de5\u5177\u7c7b\u4e2d\u7684\u9759\u6001\u65b9\u6cd5<\/td><\/tr><tr><td>\u4e00\u5806\u96f6\u6563\u6570\u636e<\/td><td>public static Stream of(T\u2026 values)<\/td><td>Stream\u63a5\u53e3\u4e2d\u7684\u9759\u6001\u65b9\u6cd5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/1.\u5355\u5217\u96c6\u5408\n        ArrayList&lt;Integer> arrayList=new ArrayList&lt;>();\n        Collections.addAll(arrayList,1,2,5,6,3,7);\n\n        arrayList.stream().forEach(s-> System.out.println(s));\n\n        \/\/2.\u53cc\u5217\u96c6\u5408\n        HashMap&lt;String,Integer> hashMap=new HashMap&lt;>();\n        hashMap.put(\"aaa\",111);\n        hashMap.put(\"bbb\",222);\n        hashMap.put(\"ccc\",333);\n        hashMap.put(\"ddd\",444);\n\n        hashMap.keySet().stream().forEach(s-> System.out.println(s));\n        hashMap.values().stream().forEach(s-> System.out.println(s));\n        hashMap.entrySet().stream().forEach(s-> System.out.println(s));\n\n        \/\/3.\u6570\u7ec4\n        int&#91;] arr={1,5,3,2,4};\n\n        Arrays.stream(arr).forEach(s-> System.out.println(s));\n\n        \/\/4.\u96f6\u6563\u6570\u636e\n        Stream.of(5,9,3,2,7).forEach(e-> System.out.println(e));<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">4.\u6d41\u7684\u4e2d\u95f4\u65b9\u6cd5<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u540d\u79f0<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>Stream filter(Predicate predicate)<\/td><td>\u8fc7\u6ee4<\/td><\/tr><tr><td>Stream limit(long maxSize)<\/td><td>\u83b7\u53d6\u524d\u51e0\u4e2a\u5143\u7d20<\/td><\/tr><tr><td>Stream skip(long n)<\/td><td>\u8df3\u8fc7\u524d\u51e0\u4e2a\u5143\u7d20<\/td><\/tr><tr><td>Stream distinct()<\/td><td>\u5143\u7d20\u53bb\u91cd\uff0c\u4f9d\u8d56(hashCode\u548cequals\u65b9\u6cd5)<\/td><\/tr><tr><td>static Stream concat(Stream a, Stream b)<\/td><td>\u5408\u5e76a\u548cb\u4e24\u4e2a\u6d41\u4e3a\u4e00\u4e2a\u6d41<\/td><\/tr><tr><td>Stream map(Function mapper)<\/td><td>\u8f6c\u6362\u6d41\u4e2d\u7684\u6570\u636e\u7c7b\u578b<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        ArrayList&lt;String> list = new ArrayList&lt;>();\n        list.add(\"Jason Hard\");\n        list.add(\"Jason Smith\");\n        list.add(\"Nick Jackson\");\n        list.add(\"Mike Trump\");\n        list.add(\"Jason Mask\");\n\n\n        list.stream()\n                .filter(e->e.startsWith(\"Jason\"))\n                .filter(s->s.length()==10)\n                .forEach(s->System.out.println(s));\/\/Jason Hard ;Jason Mask<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">5.\u6d41\u7684\u7ec8\u7ed3\u65b9\u6cd5\uff1a<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u540d\u79f0<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>void forEach(Consumer action)<\/td><td>\u904d\u5386<\/td><\/tr><tr><td>long count()<\/td><td>\u7edf\u8ba1<\/td><\/tr><tr><td>toArray()<\/td><td>\u6536\u96c6\u6d41\u4e2d\u7684\u6570\u636e\uff0c\u653e\u5230\u6570\u7ec4\u4e2d<\/td><\/tr><tr><td>collect(Collector collector)<\/td><td>\u6536\u96c6\u6d41\u4e2d\u7684\u6570\u636e\uff0c\u653e\u5230\u96c6\u5408\u4e2d<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u793a\u4f8b1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ArrayList&lt;String> list = new ArrayList&lt;>();<br>list.add(\"Jason Hard\");<br>list.add(\"Jason Smith\");<br>list.add(\"Nick Jackson\");<br>list.add(\"Mike Trump\");<br>list.add(\"Jason Mask\");<br><br>String[] arr=list.stream().toArray(value -> new String[value]);\/\/value\u662f\u6570\u7ec4\u7684\u957f\u5ea6\/\u5143\u7d20\u7684\u4e2a\u6570<br>System.<em>out<\/em>.println(Arrays.<em>toString<\/em>(arr));\/\/[Jason Hard, Jason Smith, Nick Jackson, Mike Trump, Jason Mask]<\/pre>\n\n\n\n<p>\u793a\u4f8b2\uff1acollect<\/p>\n\n\n\n<p>\u6b64\u5904split\u7684\u7528\u6cd5\uff0c\u8be6\u89c1<a href=\"http:\/\/www.cmd137blog.top\/?p=367\">JAVA-\u6838\u5fc3\u7c7b-String\u76f8\u5173 \u2013 CMD137&#8217;s Blog<\/a><\/p>\n\n\n\n<p>\u5bf9\u4e8ecollect\u4e3alist\u548cset\u7684\u533a\u522b\uff1aset\u4e2d\u4e0d\u80fd\u6709\u91cd\u590d\u7684\u5143\u7d20\u3001\u800clist\u5219\u53ef\u4ee5\u3002<\/p>\n\n\n\n<p>\u6709\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ArrayList&lt;String> list = new ArrayList&lt;>();\n        Collections.addAll(list,\n                \"\u5f20\u65e0\u5fcc-\u7537-15\", \"\u5f20\u65e0\u5fcc-\u7537-15\", \"\u5468\u82b7\u82e5-\u5973-14\", \"\u8d75\u654f-\u5973-13\",\n                \"\u5f20\u5f3a-\u7537-20\", \"\u5f20\u4e09\u4e30-\u7537-100\", \"\u5f20\u7fe0\u5c71-\u7537-40\", \"\u5f20\u826f-\u7537-35\",\n                \"\u738b\u4e8c\u9ebb\u5b50-\u7537-37\", \"\u8c22\u5e7f\u5764-\u7537-41\"\n        );<\/code><\/pre>\n\n\n\n<p>collect\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        \/\/\u6536\u96c6\u5230list\u4e2d\n        List newlist= list.stream()\n                .filter(s -> \"\u7537\".equals(s.split(\"-\")&#91;1]))\n                .collect(Collectors.toList());\n        newlist.stream().forEach(s-> System.out.println(s));\/\/\u76f8\u6bd4\u4e8eset\uff0c\u6709\u91cd\u590d\u5143\u7d20\n\n        \/\/\u6536\u96c6\u5230set\u4e2d\n        Set newset= list.stream()\n                .filter(s->s.split(\"-\")&#91;1].equals(\"\u7537\"))\n                .collect(Collectors.toSet());\n        System.out.println(newset.toString());<\/code><\/pre>\n\n\n\n<p>\u5355\u72ec\u8bf4\u660etoMap<\/p>\n\n\n\n<p>\u5bf9\u4e8e.collect(Collectors.toMap(&#8230;&#8230;&#8230;))<\/p>\n\n\n\n<p>toMap(\u952e\u7684\u89c4\u5219\uff0c\u503c\u7684\u89c4\u5219)\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53c2\u6570\u4e00\u8868\u793a\u952e\u7684\u751f\u6210\u89c4\u5219\n<ul class=\"wp-block-list\">\n<li>Function\uff1a\n<ul class=\"wp-block-list\">\n<li>\u53c2\u6570\u4e00\uff08\u6cdb\u578b\uff09\uff1a\u8868\u793a\u6d41\u4e2d\u6bcf\u4e00\u4e2a\u6570\u636e\u7684\u7c7b\u578b<\/li>\n\n\n\n<li>\u53c2\u6570\u4e8c\uff08\u6cdb\u578b\uff09\uff1a\u8868\u793a\u8981\u8f6c\u6362\u6210\u7684Map\u96c6\u5408\u4e2d\u7684<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u952e<\/mark>\u7684\u6570\u636e\u7c7b\u578b<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>apply\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5f62\u53c2\uff1a\u4f9d\u6b21\u8868\u793a\u6d41\u7684\u6bcf\u4e00\u4e2a\u6570\u636e<\/li>\n\n\n\n<li>\u65b9\u6cd5\u4f53\uff1a\u751f\u6210<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u952e<\/mark>\u7684\u4ee3\u7801\uff0c\u6700\u540e\u8981\u8fd4\u56de<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u952e<\/mark><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u952e\u53c2\u6570\u4e8c\u8868\u793a\u503c\u7684\u751f\u6210\u89c4\u5219\n<ul class=\"wp-block-list\">\n<li>Function\uff1a\n<ul class=\"wp-block-list\">\n<li>\u53c2\u6570\u4e00\uff08\u6cdb\u578b\uff09\uff1a\u8868\u793a\u6d41\u4e2d\u6bcf\u4e00\u4e2a\u6570\u636e\u7684\u7c7b\u578b<\/li>\n\n\n\n<li>\u53c2\u6570\u4e8c\uff08\u6cdb\u578b\uff09\uff1a\u8868\u793a\u8981\u8f6c\u6362\u6210\u7684Map\u96c6\u5408\u4e2d\u7684<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u503c<\/mark>\u7684\u6570\u636e\u7c7b\u578b<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>apply\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5f62\u53c2\uff1a\u4f9d\u6b21\u8868\u793a\u6d41\u7684\u6bcf\u4e00\u4e2a\u6570\u636e<\/li>\n\n\n\n<li>\u65b9\u6cd5\u4f53\uff1a\u751f\u6210<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u503c<\/mark>\u7684\u4ee3\u7801\uff0c\u6700\u540e\u8981\u8fd4\u56de<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u503c<\/mark><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    public static void main(String&#91;] args) {\n        ArrayList&lt;String> list = new ArrayList&lt;>();\n        Collections.addAll(list,\n                \"\u5f20\u65e0\u5fcc-\u7537-15\", \"\u5468\u82b7\u82e5-\u5973-14\", \"\u8d75\u654f-\u5973-13\",\n                \"\u5f20\u5f3a-\u7537-20\", \"\u5f20\u4e09\u4e30-\u7537-100\", \"\u5f20\u7fe0\u5c71-\u7537-40\", \"\u5f20\u826f-\u7537-35\",\n                \"\u738b\u4e8c\u9ebb\u5b50-\u7537-37\", \"\u8c22\u5e7f\u5764-\u7537-41\"\n        );\/\/\u6ce8\u610f\u6b64\u5904\u952e\u4e0d\u80fd\u91cd\u590d\n\n\n\n        \/\/\u6536\u96c6\u5230map\u4e2d\n        Map newmap=list.stream()\n                .filter(s -> \"\u7537\".equals(s.split(\"-\")&#91;1]))\n                .collect(Collectors.toMap(new Function&lt;String, String>() {\n                                              @Override\n                                              public String apply(String string) {\n\n                                                  return string.split(\"-\")&#91;0];\n                                              }\n                                          },\n                                            new Function&lt;String, Integer>() {\n                                                @Override\n                                                public Integer apply(String string) {\n                                                    return Integer.parseInt(string.split(\"-\")&#91;2]);\n                                                }\n                                            }));\n        System.out.println(newmap.toString());\n\/\/{\u5f20\u5f3a=20, \u5f20\u826f=35, \u5f20\u7fe0\u5c71=40, \u738b\u4e8c\u9ebb\u5b50=37, \u5f20\u4e09\u4e30=100, \u5f20\u65e0\u5fcc=15, \u8c22\u5e7f\u5764=41}\n\n    }<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u6d41\u7684\u601d\u60f3\uff1a 2.\u6d41\u7684\u4f7f\u7528\u6b65\u9aa4\uff1a 3.\u6d41\u7684\u83b7\u53d6\uff1a \u83b7\u53d6\u65b9\u5f0f \u65b9\u6cd5\u540d \u8bf4\u660e \u5355\u5217\u96c6\u5408 default Stre [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[25],"class_list":["post-491","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"_links":{"self":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=491"}],"version-history":[{"count":1,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/491\/revisions"}],"predecessor-version":[{"id":494,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/491\/revisions\/494"}],"wp:attachment":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=491"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}