{"id":2083,"date":"2025-07-31T15:41:07","date_gmt":"2025-07-31T07:41:07","guid":{"rendered":"https:\/\/note.ui11.cc\/?p=2083"},"modified":"2025-07-31T15:41:08","modified_gmt":"2025-07-31T07:41:08","slug":"stream-api","status":"publish","type":"post","link":"https:\/\/note.ui11.cc\/index.php\/2025\/07\/31\/stream-api\/","title":{"rendered":"Stream API"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1. \u521b\u5efa Stream<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4ece\u96c6\u5408\u521b\u5efa<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u96c6\u5408\u7c7b\uff08\u5982<code>List<\/code>\u3001<code>Set<\/code>\uff09\u90fd\u6709<code>stream()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u4e00\u4e2a\u987a\u5e8f\u6d41\uff0c<code>parallelStream()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u4e00\u4e2a\u5e76\u884c\u6d41\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\nimport java.util.List;\nimport java.util.stream.Stream;\n\npublic class StreamExample {\n    public static void main(String&#91;] args) {\n        List&lt;Integer> numbers = new ArrayList&lt;>();\n        numbers.add(1);\n        numbers.add(2);\n        numbers.add(3);\n        numbers.add(4);\n        numbers.add(5);\n\n        \/\/ \u521b\u5efa\u987a\u5e8f\u6d41\n        Stream&lt;Integer> stream = numbers.stream();\n\n        \/\/ \u521b\u5efa\u5e76\u884c\u6d41\n        Stream&lt;Integer> parallelStream = numbers.parallelStream();\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u987a\u5e8f\u6d41<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b9a\u4e49\u4e0e\u6267\u884c\u65b9\u5f0f<\/strong>\uff1a\u987a\u5e8f\u6d41\u6309\u7167\u5143\u7d20\u5728\u6570\u636e\u6e90\u4e2d\u7684\u987a\u5e8f\u4f9d\u6b21\u5904\u7406\u6bcf\u4e2a\u5143\u7d20\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u6d41\u4e2d\u7684\u6bcf\u4e00\u4e2a\u64cd\u4f5c\uff0c\u90fd\u662f\u524d\u4e00\u4e2a\u5143\u7d20\u5904\u7406\u5b8c\u6210\u540e\uff0c\u518d\u5904\u7406\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5f53\u5bf9\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5143\u7d20\u7684\u5217\u8868\u521b\u5efa\u987a\u5e8f\u6d41\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u8fc7\u6ee4\u64cd\u4f5c\u65f6\uff0c\u4f1a\u4ece\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u9010\u4e2a\u5224\u65ad\u662f\u5426\u6ee1\u8db3\u8fc7\u6ee4\u6761\u4ef6\uff0c\u5904\u7406\u5b8c\u7b2c\u4e00\u4e2a\u5143\u7d20\u540e\u63a5\u7740\u5904\u7406\u7b2c\u4e8c\u4e2a\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e76\u884c\u6d41<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b9a\u4e49\u4e0e\u6267\u884c\u65b9\u5f0f<\/strong>\uff1a\u5e76\u884c\u6d41\u5229\u7528\u591a\u7ebf\u7a0b\u5e76\u884c\u5904\u7406\u6d41\u4e2d\u7684\u5143\u7d20\u3002\u5b83\u5c06\u6d41\u4e2d\u7684\u6570\u636e\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u6bcf\u4e2a\u90e8\u5206\u7531\u4e00\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\u8fdb\u884c\u5904\u7406\uff0c\u4ece\u800c\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u7684\u8ba1\u7b97\u80fd\u529b\uff0c\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002\u4f8b\u5982\uff0c\u5bf9\u4e00\u4e2a\u5927\u6570\u636e\u96c6\u521b\u5efa\u5e76\u884c\u6d41\u5e76\u8fdb\u884c\u8fc7\u6ee4\u64cd\u4f5c\u65f6\uff0c\u6570\u636e\u96c6\u4f1a\u88ab\u5206\u6210\u591a\u4e2a\u5b50\u6570\u636e\u96c6\uff0c\u6bcf\u4e2a\u5b50\u6570\u636e\u96c6\u7531\u4e0d\u540c\u7684\u7ebf\u7a0b\u5e76\u884c\u5730\u8fdb\u884c\u8fc7\u6ee4\u5904\u7406\u3002<\/li>\n<\/ol>\n\n\n\n<p>demo\uff1a<br>public class StreamComparison {<br>    public static void main(String[] args) {<br>        List&lt;Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);<br>        \/\/ \u987a\u5e8f\u6d41\u64cd\u4f5c<br>        long sequentialStartTime = System.currentTimeMillis();<br>        List&lt;Integer> sequentialResult = numbers.stream()<br>               .map(n -> n * n)<br>               .collect(Collectors.toList());<br>        long sequentialEndTime = System.currentTimeMillis();<br>        System.out.println(&#8220;\u987a\u5e8f\u6d41\u7ed3\u679c: &#8221; + sequentialResult);<br>        System.out.println(&#8220;\u987a\u5e8f\u6d41\u5904\u7406\u65f6\u95f4: &#8221; + (sequentialEndTime &#8211; sequentialStartTime) + &#8221; \u6beb\u79d2&#8221;);<br><br>        \/\/ \u5e76\u884c\u6d41\u64cd\u4f5c<br>        long parallelStartTime = System.currentTimeMillis();<br>        List&lt;Integer> parallelResult = numbers.parallelStream()<br>               .map(n -> n * n)<br>               .collect(Collectors.toList());<br>        long parallelEndTime = System.currentTimeMillis();<br>        System.out.println(&#8220;\u5e76\u884c\u6d41\u7ed3\u679c: &#8221; + parallelResult);<br>        System.out.println(&#8220;\u5e76\u884c\u6d41\u5904\u7406\u65f6\u95f4: &#8221; + (parallelEndTime &#8211; parallelStartTime) + &#8221; \u6beb\u79d2&#8221;);<br>    }<br>}<\/p>\n\n\n\n<p>example\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Override\n    public Map&lt;String, Object> qryStaffDetailInfo(Map&lt;String, Object> params) throws Exception {\n        Map retMap = new HashMap&lt;String, Object>();\n        \/\/ \u83b7\u53d6\u5458\u5de5\u4e3b\u952e\n        String staffId = (String) params.get(\"staffId\");\n        BizObject bizObject = MetaDaoHelper.findById(\"hred.staff.Staff\", Long.parseLong(staffId), \"hrcloud-staff-mgr\");\n        List&lt;BizObject> staffJobList = bizObject.getBizObjects(\"staffJob\", BizObject.class);\n        List&lt;BizObject> filterStaffJobList = new ArrayList&lt;>();\n        List&lt;BizObject> filterStaffEduList = new ArrayList&lt;>();\n        List&lt;BizObject> filterStaffOrgRelList = new ArrayList&lt;>();\n        if (CollectionUtils.isNotEmpty(staffJobList)) {\n            filterStaffJobList = staffJobList.stream().filter(t -> (Integer) t.get(\"dr\") == 0 &amp;&amp; (Boolean) t.get(\"isMainJob\") &amp;&amp; t.get(\"endDate\") == null).collect(Collectors.toList());\n        }\n        List&lt;BizObject> staffEduList = bizObject.getBizObjects(\"staffEdu\", BizObject.class);\n        if (CollectionUtils.isNotEmpty(staffEduList)) {\n            filterStaffEduList = staffEduList.stream().filter(t -> (Integer) t.get(\"dr\") == 0 &amp;&amp; (Boolean) t.get(\"isPrefs\")).collect(Collectors.toList());\n        }\n        List&lt;BizObject> staffOrgRelList = bizObject.getBizObjects(\"staffOrgRel\", BizObject.class);\n        if (CollectionUtils.isNotEmpty(staffOrgRelList)) {\n            filterStaffOrgRelList = staffOrgRelList.stream().filter(t -> (Integer) t.get(\"dr\") == 0 &amp;&amp; t.get(\"endDate\") == null).collect(Collectors.toList());\n        }\n        retMap.put(\"staffJob\", filterStaffJobList);\n        retMap.put(\"staffEdu\", filterStaffEduList);\n        retMap.put(\"staffOrgRel\", filterStaffOrgRelList);\n        return retMap;\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>staffJobList.stream()<\/code><\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u4e00\u6b65\u5c06\u00a0<code>staffJobList<\/code>\uff08\u4e00\u4e2a\u00a0<code>List&lt;BizObject><\/code>\uff09\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6d41\uff08<code>Stream&lt;BizObject><\/code>\uff09\u3002\u6d41\u662f\u4e00\u79cd\u652f\u6301\u987a\u5e8f\u548c\u5e76\u884c\u805a\u5408\u64cd\u4f5c\u7684\u5143\u7d20\u5e8f\u5217\uff0c\u5b83\u5141\u8bb8\u4ee5\u4e00\u79cd\u58f0\u660e\u5f0f\u7684\u65b9\u5f0f\u5bf9\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u5904\u7406\u3002\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6d41\uff0c\u540e\u7eed\u53ef\u4ee5\u4f7f\u7528 Stream API \u63d0\u4f9b\u7684\u4e30\u5bcc\u64cd\u4f5c\uff0c\u5982\u8fc7\u6ee4\u3001\u6620\u5c04\u3001\u6392\u5e8f\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>.filter(t -> (Integer) t.get(\"dr\") == 0 &amp;&amp; (Boolean) t.get(\"isMainJob\") &amp;&amp; t.get(\"endDate\") == null)<\/code><\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>filter<\/code>\u00a0\u662f Stream API \u4e2d\u7684\u4e00\u4e2a\u4e2d\u95f4\u64cd\u4f5c\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u00a0<code>Predicate<\/code>\uff08\u65ad\u8a00\uff09\u4f5c\u4e3a\u53c2\u6570\uff0c\u7528\u4e8e\u5bf9\u6d41\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u7b5b\u9009\u3002\u53ea\u6709\u6ee1\u8db3\u65ad\u8a00\u6761\u4ef6\u7684\u5143\u7d20\u624d\u4f1a\u88ab\u4fdd\u7559\u5728\u65b0\u7684\u6d41\u4e2d\u3002<\/li>\n\n\n\n<li><code>t -> (Integer) t.get(\"dr\") == 0 &amp;&amp; (Boolean) t.get(\"isMainJob\") &amp;&amp; t.get(\"endDate\") == null<\/code>\u00a0\u662f\u4e00\u4e2a Lambda \u8868\u8fbe\u5f0f\uff0c\u5b83\u5b9a\u4e49\u4e86\u8fc7\u6ee4\u7684\u6761\u4ef6\u3002\u8fd9\u91cc\u7684\u00a0<code>t<\/code>\u00a0\u4ee3\u8868\u6d41\u4e2d\u7684\u6bcf\u4e2a\u00a0<code>BizObject<\/code>\u00a0\u5143\u7d20\u3002<\/li>\n\n\n\n<li><code>(Integer) t.get(\"dr\") == 0<\/code>\uff1a<code>t.get(\"dr\")<\/code>\u00a0\u4ece\u00a0<code>BizObject<\/code>\u00a0\u4e2d\u83b7\u53d6\u540d\u4e3a\u00a0<code>dr<\/code>\u00a0\u7684\u5c5e\u6027\u503c\uff0c\u5e76\u5c06\u5176\u5f3a\u5236\u8f6c\u6362\u4e3a\u00a0<code>Integer<\/code>\u00a0\u7c7b\u578b\uff0c\u7136\u540e\u5224\u65ad\u8be5\u503c\u662f\u5426\u7b49\u4e8e\u00a0<code>0<\/code>\u3002\u8fd9\u91cc\u7684\u00a0<code>dr<\/code>\u00a0\u53ef\u80fd\u662f\u4e00\u4e2a\u7528\u4e8e\u6807\u8bb0\u6570\u636e\u72b6\u6001\u7684\u5b57\u6bb5\uff0c\u4f8b\u5982\u00a0<code>0<\/code>\u00a0\u53ef\u80fd\u8868\u793a\u6709\u6548\u6570\u636e\u3002<\/li>\n\n\n\n<li><code>(Boolean) t.get(\"isMainJob\")<\/code>\uff1a\u83b7\u53d6\u540d\u4e3a\u00a0<code>isMainJob<\/code>\u00a0\u7684\u5c5e\u6027\u503c\u5e76\u5f3a\u5236\u8f6c\u6362\u4e3a\u00a0<code>Boolean<\/code>\u00a0\u7c7b\u578b\uff0c\u5224\u65ad\u5176\u662f\u5426\u4e3a\u00a0<code>true<\/code>\u3002\u8fd9\u4e2a\u5c5e\u6027\u53ef\u80fd\u7528\u4e8e\u6807\u8bc6\u8be5\u5de5\u4f5c\u662f\u5426\u4e3a\u5458\u5de5\u7684\u4e3b\u8981\u5de5\u4f5c\u3002<\/li>\n\n\n\n<li><code>t.get(\"endDate\") == null<\/code>\uff1a\u68c0\u67e5\u00a0<code>BizObject<\/code>\u00a0\u4e2d\u540d\u4e3a\u00a0<code>endDate<\/code>\u00a0\u7684\u5c5e\u6027\u503c\u662f\u5426\u4e3a\u00a0<code>null<\/code>\u3002\u8fd9\u53ef\u80fd\u8868\u793a\u8be5\u5de5\u4f5c\u7684\u7ed3\u675f\u65e5\u671f\u4e3a\u7a7a\uff0c\u5373\u5f53\u524d\u5de5\u4f5c\u4ecd\u5728\u8fdb\u884c\u4e2d\u3002<\/li>\n\n\n\n<li>\u8fd9\u4e09\u4e2a\u6761\u4ef6\u901a\u8fc7\u00a0<code>&amp;&amp;<\/code>\uff08\u903b\u8f91\u4e0e\uff09\u8fd0\u7b97\u7b26\u8fde\u63a5\uff0c\u610f\u5473\u7740\u53ea\u6709\u5f53\u4e00\u4e2a\u00a0<code>BizObject<\/code>\u00a0\u540c\u65f6\u6ee1\u8db3\u8fd9\u4e09\u4e2a\u6761\u4ef6\u65f6\uff0c\u5b83\u624d\u4f1a\u88ab\u4fdd\u7559\u5728\u7ecf\u8fc7\u8fc7\u6ee4\u540e\u7684\u6d41\u4e2d\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>.collect(Collectors.toList())<\/code><\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>collect<\/code>\u00a0\u662f Stream API \u4e2d\u7684\u4e00\u4e2a\u7ec8\u6b62\u64cd\u4f5c\uff0c\u5b83\u5c06\u6d41\u4e2d\u7684\u5143\u7d20\u7d2f\u79ef\u5230\u4e00\u4e2a\u53ef\u53d8\u7684\u7ed3\u679c\u5bb9\u5668\u4e2d\uff0c\u8fd9\u91cc\u4f7f\u7528\u00a0<code>Collectors.toList()<\/code>\u00a0\u5c06\u8fc7\u6ee4\u540e\u7684\u6d41\u4e2d\u7684\u5143\u7d20\u6536\u96c6\u5230\u4e00\u4e2a\u65b0\u7684\u00a0<code>List<\/code>\u00a0\u4e2d\u3002<code>Collectors<\/code>\u00a0\u7c7b\u63d0\u4f9b\u4e86\u8bb8\u591a\u9759\u6001\u65b9\u6cd5\u6765\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u6536\u96c6\u5668\uff0c<code>toList()<\/code>\u00a0\u65b9\u6cd5\u521b\u5efa\u7684\u6536\u96c6\u5668\u4f1a\u5c06\u6d41\u4e2d\u7684\u5143\u7d20\u6536\u96c6\u5230\u4e00\u4e2a\u00a0<code>List<\/code>\u00a0\u4e2d\u3002\u6700\u7ec8\uff0c\u8fd9\u4e2a\u65b0\u7684\u00a0<code>List<\/code>\u00a0\u88ab\u8d4b\u503c\u7ed9\u00a0<code>filterStaffJobList<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u521b\u5efa Stream \u987a\u5e8f\u6d41 \u5e76\u884c\u6d41 demo\uff1apublic class StreamComparison [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,3,17],"tags":[],"class_list":["post-2083","post","type-post","status-publish","format-standard","hentry","category-fileio","category-javase","category-17"],"_links":{"self":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/2083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/comments?post=2083"}],"version-history":[{"count":1,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/2083\/revisions"}],"predecessor-version":[{"id":2084,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/2083\/revisions\/2084"}],"wp:attachment":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/media?parent=2083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/categories?post=2083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/tags?post=2083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}