{"id":970,"date":"2021-09-15T20:31:44","date_gmt":"2021-09-15T12:31:44","guid":{"rendered":"https:\/\/fireinsect.top\/?p=970"},"modified":"2021-09-17T19:37:03","modified_gmt":"2021-09-17T11:37:03","slug":"java%e5%b9%b6%e5%8f%91%ef%bc%88stream-api%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2021\/09\/15\/java%e5%b9%b6%e5%8f%91%ef%bc%88stream-api%ef%bc%89\/","title":{"rendered":"Java\u5e76\u53d1\uff08Stream API\uff09"},"content":{"rendered":"\n<p><strong>Stream(\u6d41)<\/strong> \u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5bf9 \u96c6\u5408(Collection) \u4e2d\u7684\u6570\u636e\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u589e\u52a0\u4e86\u96c6\u5408\u5bf9\u8c61\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>Stream<\/strong> \u7ecf\u5e38\u4e0e <strong>Lambda<\/strong> \u4e00\u8d77\u4f7f\u7528\uff0c\u8fd9\u91cc\u7684\u6d41\u7684Java8\u7684\u65b0\u7279\u6027\uff0c\u4e0eJava\u539f\u672c\u7684\u6587\u4ef6\u6d41\u662f\u5b8c\u5168\u4e0d\u540c\u7684\u3002<\/p>\n\n\n\n<p>\u6570\u636e\u6d41\u7684\u64cd\u4f5c\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u770b\u505a\u4e00\u4e2a\u7ba1\u9053\uff0c\u7ba1\u9053\u7531\u591a\u4e2a\u8282\u70b9\u7ec4\u6210\uff0c\u6bcf\u4e2a\u8282\u70b9\u5b8c\u6210\u4e00\u4e2a\u64cd\u4f5c\u3002\u6570\u636e\u6d41\u8f93\u5165\u8fd9\u4e2a\u7ba1\u9053\uff0c\u6309\u7167\u987a\u5e8f\u7ecf\u8fc7\u6bcf\u4e2a\u8282\u70b9\uff0c\u8fd9\u79cd\u4e0b\u4e00\u4e2a\u8282\u70b9\u5fc5\u987b\u7b49\u5f85\u4e0a\u4e00\u4e2a\u8282\u70b9\u6267\u884c\u5b8c\u6bd5\u7684\u65b9\u6cd5\u53eb\u505a\u4e32\u884c\u3002<\/p>\n\n\n\n<p>\u6570\u636e\u6d41\u7684\u91cd\u70b9\u4e0d\u518d\u662f\u5bf9\u8c61\u7684\u8fd0\u7528\uff0c\u800c\u662f\u6570\u636e\u7684\u8ba1\u7b97\uff0c\u5176\u7279\u5f81\u662f\uff1a\u51fd\u6570\u5f0f\u98ce\u683c\uff0c\u5373\u5f31\u5316\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u4e25\u683c\u3001\u5b8c\u6574\u7684\u8bed\u6cd5\uff0c\u91cd\u5fc3\u53d8\u4e3a\u901a\u8fc7\u51fd\u6570\u5b8c\u6210\u6570\u636e\u8ba1\u7b97\u3002<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u6d41\u7684\u521b\u5efa<\/p>\n\n\n\n<p>\u6d41\u7684\u521b\u5efa\u6709\u597d\u591a\u79cd<\/p>\n\n\n\n<p style=\"font-size:25px\">\u76f4\u63a5\u521b\u5efa<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">Stream&lt;String&gt; stream = Stream.of(\"\u82f9\u679c\", \"\u54c8\u5bc6\u74dc\", \"\u9999\u8549\", \"\u897f\u74dc\", \"\u706b\u9f99\u679c\");<\/code><\/pre>\n\n\n\n<p style=\"font-size:25px\">\u7531\u6570\u7ec4\u8f6c\u6362<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">String[] fruitArray = <strong>new<\/strong> String[] {\"\u82f9\u679c\", \"\u54c8\u5bc6\u74dc\", \"\u9999\u8549\", \"\u897f\u74dc\", \"\u706b\u9f99\u679c\"};\nStream&lt;String&gt; stream = Stream.of(fruitArray);<\/code><\/pre>\n\n\n\n<p style=\"font-size:25px\">\u7531\u96c6\u5408\u8f6c\u6362<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">List&lt;String&gt; fruits = <strong>new<\/strong> ArrayList&lt;&gt;();\nfruits.add(\"\u82f9\u679c\");\nfruits.add(\"\u54c8\u5bc6\u74dc\");\nfruits.add(\"\u9999\u8549\");\nfruits.add(\"\u897f\u74dc\");\nfruits.add(\"\u706b\u9f99\u679c\");\nStream&lt;String&gt; stream = fruits.stream();<\/code><\/pre>\n\n\n\n<p>Stream\u53ef\u4ee5\u5229\u7528forEach()\u65b9\u6cd5\u8fed\u4ee3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">Stream&lt;String&gt; stream = Stream.of(\"\u82f9\u679c\", \"\u54c8\u5bc6\u74dc\", \"\u9999\u8549\", \"\u897f\u74dc\", \"\u706b\u9f99\u679c\");\nstream.forEach(System.out::println);<\/code><\/pre>\n\n\n\n<div style=\"height:18px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u805a\u5408\u64cd\u4f5c<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\" style=\"font-size:25px\">filter()\u65b9\u6cd5<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4f5c\u7528\u662f\u5bf9\u6570\u636e\u5bf9\u8c61\u8fdb\u884c\u8fc7\u6ee4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">pupils.stream()\n    .filter(pupil -&gt; pupil.getAverageScore() &gt;= 80 &amp;&amp; pupil.getViolationCount() &lt; 1)<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528 <strong>Lambda<\/strong> \u8bed\u53e5\u544a\u8bc9\u8fc7\u6ee4\u5668\uff0c\u9700\u8981\u54ea\u4e9b\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e<\/p>\n\n\n\n<p>\u8fd9\u91cc\u4e0e <strong>Lambda<\/strong> \u8bed\u53e5\u4e0d\u540c\u7684\u662f\uff0c\u56e0\u4e3a\u8fc7\u6ee4\u6761\u4ef6\u8bed\u53e5\u4e3a\u975e\u53ef\u6267\u884c\u8bed\u53e5\uff0c\u5199\u5728\u5c0f\u62ec\u53f7\uff08\uff09\u4e2d\uff0c\u800c\u4e0d\u662f\u5199\u5728{} \u4e2d<\/p>\n\n\n\n<div style=\"height:12px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\" style=\"font-size:25px\">map()\u65b9\u6cd5<\/p>\n\n\n\n<p><strong>map()<\/strong>\u65b9\u6cd5\u901a\u79f0\u6620\u5c04\uff0c\u4f5c\u7528\u4e3a\u7528\u65b0\u7684\u5143\u7d20\u5c06\u6d41\u4e2d\u539f\u672c\u76f8\u540c\u4f4d\u7f6e\u7684\u5143\u7d20\u66ff\u6362\u6389\uff0c\u76f8\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u5bf9\u8c61\u90fd\u7ecf\u5386\u4ee5\u6b64\u8f6c\u6362\u3002<\/p>\n\n\n\n<p>\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">numbers.stream()\n    .map(num -&gt; {\n        <strong>return<\/strong> num * num;\n    })\n    .forEach(System.out::println);<\/code><\/pre>\n\n\n\n<p><code>map()<\/code>&nbsp;\u65b9\u6cd5\u7684\u53c2\u6570\u662f\u4e00\u4e2a&nbsp;<em>Lambda<\/em>&nbsp;\u8868\u8fbe\u5f0f\uff0c\u5728\u8bed\u53e5\u5757\u4e2d\u5bf9\u6d41\u4e2d\u7684\u6bcf\u4e2a\u6570\u636e\u5bf9\u8c61\u8fdb\u884c\u8ba1\u7b97\u3001\u5904\u7406\uff0c\u6700\u540e\u7528&nbsp;<code>return<\/code>&nbsp;\u8bed\u53e5\u8fd4\u56de\u7684\u5bf9\u8c61\uff0c\u5c31\u662f\u8f6c\u6362\u540e\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u6620\u5c04\u540e\u7684\u5bf9\u8c61\u7c7b\u578b\u53ef\u4ee5\u4e0e\u6d41\u4e2d\u539f\u59cb\u7684\u5bf9\u8c61\u7c7b\u578b\u4e0d\u4e00\u81f4\uff0c<\/p>\n\n\n\n<p>\u5982\u5728\u6d41\u4e2d\uff0c\u53ef\u4ee5\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u539f\u6765\u7684\u6574\u6570\u3002\u8fd9\u5c31\u6781\u5927\u7684\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\u548c\u62d3\u5c55\u6027\uff0c\u8ba9\u6d41\u540e\u7ee7\u7684\u64cd\u4f5c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u3002<\/p>\n\n\n\n<p>\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u66ff\u6362\u8bed\u53e5\u7b80\u5355\u3001\u7cfb\u7edf\u80fd\u81ea\u52a8\u8bc6\u522b\u9700\u8981\u8fd4\u56de\u7684\u503c\uff0c\u4ee3\u7801\u53ef\u4ee5\u7b80\u5199\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">.map(num -&gt; num * num)<\/code><\/pre>\n\n\n\n<p>\u5f53\u7136\uff0c\u6700\u597d\u8fd8\u662f\u4f7f\u7528 return \u6765\u5b8c\u6210\u6620\u5c04\u3002<\/p>\n\n\n\n<div style=\"height:22px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\" style=\"font-size:25px\">sorted()\u65b9\u6cd5<\/p>\n\n\n\n<p>sorted()\u65b9\u6cd5\u5c31\u662f\u7528\u6765\u6392\u5e8f\u7684\u65b9\u6cd5\uff0c\u628a\u6392\u5e8f\u89c4\u5219\u5199\u6210\u4e00\u4e2a Lambda\u8868\u8fbe\u5f0f\u4f20\u7ed9\u6b64\u65b9\u6cd5\u5373\u53ef\uff0c\u7c7b\u4f3c\u4e8e Collections\u7684sort\u65b9\u6cd5\uff0c\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">students.stream()\n    <em>\/\/ \u5b9e\u73b0\u5347\u5e8f\u6392\u5e8f<\/em>\n    .sorted((student1, student2) -&gt; {\n        <strong>return<\/strong> student1.getRollNo() - student2.getRollNo();\n    })\n    .forEach(System.out::println);<\/code><\/pre>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-medium-font-size\">\u6ce8\u610f\u7684\u662f <strong>.sorted((student1, student2) <\/strong>\u4f7f\u7528\u65f6\uff0cstudent1\u6307\u4ee3\u7684\u662f\u540e\u4e00\u4e2a\u5143\u7d20\uff0cstudent2\u6307\u4ee3\u7684\u662f\u524d\u4e00\u4e2a\u5143\u7d20\uff01<\/p>\n\n\n\n<p>\u5982\u679c\u8bed\u53e5\u7b80\u5355\uff0c\u7cfb\u7edf\u53ef\u4ee5\u81ea\u52a8\u8bc6\u522b\uff0c\u90a3\u4e48\u4ee3\u7801\u53ef\u4ee5\u7b80\u5199<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">.sorted((student1, student2) -&gt; student1.getRollNo() - student2.getRollNo())<\/code><\/pre>\n\n\n\n<p>\u5f53\u7136\u5efa\u8bae\u8fd8\u662f\u5199\u5b8c\u6574\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\uff0csort\u65b9\u6cd5\u8fd4\u56de\u975e\u6b63\u6570\u5219\u4e24\u4e2a\u76f8\u6bd4\u8f83\u7684\u5143\u7d20\u9700\u8981\u4ea4\u6362\u4f4d\u7f6e\uff0c\u8fd4\u56de\u4e3a\u6b63\u6570\u5219\u4e0d\u9700\u8981\u3002<\/p>\n\n\n\n<div style=\"height:28px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\" style=\"font-size:25px\">limit() \u65b9\u6cd5<\/p>\n\n\n\n<p>limit()\u65b9\u6cd5\u4f5c\u7528\u662f\u8fd4\u56de\u6d41\u7684\u524d n \u4e2a\u5143\u7d20\uff0c\u5f53\u7136 n \u4e0d\u80fd\u4e3a\u8d1f\u6570\u3002\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">numbers.stream()\n    .sorted((n1, n2) -&gt; n2 - n1)\n    .limit(3)\n    .forEach(System.out::println);<\/code><\/pre>\n\n\n\n<div style=\"height:14px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\" style=\"font-size:25px\">reduce()\u65b9\u6cd5<\/p>\n\n\n\n<p>reduce()\u65b9\u6cd5\u7684\u4f5c\u7528\u662f\u5408\u5e76\u4e86\u6240\u6709\u7684\u5143\u7d20\uff0c\u7ec8\u6b62\u8ba1\u7b97\u51fa\u4e00\u4e2a\u7ed3\u679c\uff0c\u7ec8\u6b62\u6307\u6d41\u5df2\u7ecf\u5230\u8fbe\u4e86\u7ec8\u70b9\u7ed3\u675f\u4e86\uff0c\u4e0d\u518d\u7ee7\u7eed\u6d41\u52a8\u3002<\/p>\n\n\n\n<p>forEach()\u4e5f\u662f\u6d41\u7684\u7ec8\u70b9\u3002<\/p>\n\n\n\n<p>reduce()\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6bd4\u8f83\u590d\u6742\u7684\u5bf9\u8c61\uff0c\u9700\u8981\u8c03\u7528 get()\u65b9\u6cd5\u8fd4\u56de\u6700\u7ec8\u7684\u6574\u6570\u503c<\/p>\n\n\n\n<p>\u540c\u7406\uff0c <strong>get()<\/strong>\u65b9\u6cd5\u7684\u8fd4\u56de\u7c7b\u578b\u662f\u7cfb\u7edf\u81ea\u52a8\u6839\u636e\u6d41\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\u63a8\u5b9a\u7684\u3002\u5982\u6c42\u548c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>int<\/strong> sum = numbers.stream()\n    .reduce((a, b) -&gt; a + b)\n    .get();\n<\/code><\/pre>\n\n\n\n<p><strong>reduce()<\/strong> \u65b9\u6cd5\u7684\u53c2\u6570\uff1a <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>a<\/code>&nbsp;\u5728\u7b2c\u4e00\u6b21\u6267\u884c\u8ba1\u7b97\u8bed\u53e5&nbsp;<code>a + b<\/code>&nbsp;\u65f6\uff0c\u6307\u4ee3\u6d41\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff1b\u7136\u540e\u5145\u5f53\u7f13\u5b58\u4f5c\u7528\u4ee5\u5b58\u653e\u672c\u6b21\u8ba1\u7b97\u7ed3\u679c\u3002\u6b64\u540e\u6267\u884c\u8ba1\u7b97\u8bed\u53e5\u65f6\uff0c<code>a<\/code>&nbsp;\u7684\u503c\u5c31\u662f\u4e0a\u4e00\u6b21\u7684\u8ba1\u7b97\u7ed3\u679c\u5e76\u7ee7\u7eed\u5145\u5f53\u7f13\u5b58\u5b58\u653e\u672c\u6b21\u8ba1\u7b97\u7ed3\u679c\u3002<\/li><li><code>b<\/code>&nbsp;\u53c2\u6570\u7b2c\u4e00\u6b21\u6267\u884c\u8ba1\u7b97\u8bed\u53e5\u65f6\u6307\u4ee3\u6d41\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u3002\u6b64\u540e\u4f9d\u6b21\u6307\u4ee3\u6d41\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u6ce8\u610f\uff1aa\u3001b \u4e24\u4e2a\u53c2\u6570\u7684\u4f5c\u7528\u662f\u7531<em><strong>\u4f4d\u7f6e<\/strong><\/em>\u51b3\u5b9a\u7684\uff0c\u53d8\u91cf\u540d\u662f\u4efb\u610f\u7684<\/p><\/blockquote>\n\n\n\n<p>\u7ed3\u5408\u4e0b\u56fe\u7406\u89e3\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/style.youkeda.com\/img\/ham\/course\/j5\/j5-3-1-1.svg\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5373\u524d\u4e00\u4e2a\u53c2\u6570\uff08a\uff09\u5373\u4e3a\u5faa\u73af\u7684\u96c6\u5408<\/p>\n\n\n\n<p><code>reduce()<\/code>&nbsp;\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff08\u672c\u4f8b\u7684&nbsp;<code>a<\/code>\uff09\u6709\u591a\u91cd\u4f5c\u7528\uff0c\u5e76\u4e14\u7cfb\u7edf\u662f\u81ea\u52a8\u5b8c\u6210\u53c2\u6570\uff08\u672c\u4f8b\u7684&nbsp;<code>a, b<\/code>\uff09\u8d4b\u503c\u7684\uff0c\u6240\u4ee5\u4ecd\u7136\u4f53\u73b0\u4e86&nbsp;<code>Stream<\/code>&nbsp;\u7f16\u7a0b\u7684\u91cd\u70b9\u4ecd\u7136\u662f<em><strong>\u8ba1\u7b97<\/strong><\/em>\uff08\u672c\u4f8b\u7684&nbsp;<code>a + b<\/code>\uff09\u3002<\/p>\n\n\n\n<p>reduce()\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u64cd\u4f5c\u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">Student result = students.stream().reduce(\n        (a, b) -&gt; {\n            a.setMidtermScore(a.getMidtermScore() + b.getMidtermScore());\n            <strong>return<\/strong> a;\n        }\n    )\n    .get();\n\nSystem.out.println(result.getName() + \" - \" + result.getMidtermScore());<\/code><\/pre>\n\n\n\n<p>\u4f46\u5229\u7528\u4e0a\u9762\u65b9\u6cd5\u4f1a\u5bfc\u81f4\u96c6\u5408\u7b2c\u4e00\u4e2a\u5143\u7d20\u88ab\u5f53\u505a\u4e86\u7f13\u5b58\u89d2\u8272\uff0c\u6b63\u786e\u6027\u88ab\u7834\u574f<\/p>\n\n\n\n<p>\u4e00\u822c\u6211\u4eec\u4f7f\u7528\u64cd\u4f5c\u5bf9\u8c61\uff0c\u90fd\u4f1a\u7528reduce() \u7684\u53e6\u4e00\u4e2a\u53c2\u6570\uff0c\u81ea\u5df1 <strong>new<\/strong> \u4e00\u4e2a\u5bf9\u8c61\u5145\u5f53\u7f13\u51b2\u89d2\u8272\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">Student result = students.stream()\n    .reduce(<strong>new<\/strong> Student(\"\", 0),\n        (a, b) -&gt; {\n            a.setMidtermScore(a.getMidtermScore() + b.getMidtermScore());\n            <strong>return<\/strong> a;\n        }\n    );\n\nSystem.out.println(result.getName() + \" - \" + result.getMidtermScore());<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91ccreduce\u65b9\u6cd5\u53c2\u6570\u591a\u52a0\u4e0a\u4e86\u4e00\u4e2a\u8868\u793a\u7f13\u51b2\u89d2\u8272\u3002\u800c\u4e4b\u540e\u7684a\u6307\u4ee3\u7b2c\u4e00\u6b21\u4e3a\u7f13\u5b58<\/p>\n\n\n\n<div style=\"height:27px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\" style=\"font-size:25px\">\u6d41\u6536\u96c6 collect()\u65b9\u6cd5<\/p>\n\n\n\n<p>collect()\u65b9\u6cd5\u7684\u4f5c\u7528\u5c31\u662f\u6536\u96c6\u5143\u7d20\uff0c\u4f46\u5143\u7d20\u5b58\u653e\u5728\u54ea\u5462\uff1fCollection.toList()\u662f\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\uff0c\u4f5c\u4e3a\u53c2\u6570\u544a\u8bc9collect()\u65b9\u6cd5\u5b58\u5165\u4e00\u4e2aList\u96c6\u5408\uff0c\u6240\u4ee5collect()\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u5c31\u662f List\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">List&lt;String&gt; numResult = numbers.stream()\n    .sorted((n1, n2) -&gt; n2 - n1)\n    .limit(3)\n    .map(a -&gt; \"\" + a)\n    .collect(Collectors.toList());<\/code><\/pre>\n\n\n\n<div style=\"height:21px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">\u5e76\u884c\u6d41<\/p>\n\n\n\n<p>\u4e0a\u9762\u6211\u4eec\u6240\u5b66\u7684\u4e32\u884c\u6d41\u5982\u679c\u8ba1\u7b97\u8fc7\u7a0b\u8d8a\u590d\u6742\uff0c\u6570\u636e\u91cf\u5c31\u8d8a\u5927\uff0c\u4e32\u884c\u6027\u80fd\u4f1a\u8d8a\u6765\u8d8a\u4f4e\uff0c\u800c\u8fd9\u79cd\u6a21\u5f0f\u65e0\u6cd5\u53d1\u6325\u591a\u6838CPU\u7684\u4f18\u52bf\u800c\u96be\u4ee5\u4f18\u5316\u3002<\/p>\n\n\n\n<p>\u4e3a\u6b64\u6211\u4eec\u9700\u8981\u7528\u5230\u5e76\u884c\u6d41\u3002<\/p>\n\n\n\n<p>\u5e76\u884c\u6d41\u7684\u4f7f\u7528\u662f\u6dfb\u52a0 <strong>.parallelStream()<\/strong>\u65b9\u6cd5\u4e0e\u4e32\u884c\u6d41\u7684 <strong>.stream()<\/strong>\u65b9\u6cd5\u5411\u5bf9\u5e94\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/style.youkeda.com\/img\/ham\/course\/j5\/j5-3-4-1.svg\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\">\u4f46\u5982\u679c\u6d41\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u4e4b\u95f4\u5b58\u5728\u903b\u8f91\u4f9d\u8d56\u5173\u7cfb\uff0c\u5219\u4e0d\u9002\u5408\u4f7f\u7528\u5e76\u884c\u6d41\u8ba1\u7b97<\/p>\n\n\n\n<p>\u5982\u60f3\u8981\u8ba9\u6570\u5b57\uff081,2,3,4,5\uff09\u6309\u987a\u5e8f\u8f93\u51fa\uff0c\u56e0\u4e3a\u5e76\u884c\u6d41\u7684\u8f93\u51fa\u65f6\u673a\u662fCPU\u52a8\u6001\u51b3\u5b9a\u7684\uff0c\u65e0\u6cd5\u786e\u5b9a\uff0c\u6240\u4ee5\u4e0d\u80fd\u4f7f\u7528\u5e76\u884c\u8fd0\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Stream(\u6d41) \u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5bf9 \u96c6\u5408(Collection) \u4e2d\u7684\u6570\u636e\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u589e\u52a0\u4e86\u96c6\u5408\u5bf9\u8c61\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[6],"tags":[],"class_list":["post-970","post","type-post","status-publish","format-standard","hentry","category-6"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/970"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=970"}],"version-history":[{"count":20,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/970\/revisions"}],"predecessor-version":[{"id":995,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/970\/revisions\/995"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}