{"id":111,"date":"2022-02-02T19:51:31","date_gmt":"2022-02-02T19:51:31","guid":{"rendered":"https:\/\/liangqi.org\/?p=111"},"modified":"2022-02-02T22:32:56","modified_gmt":"2022-02-02T22:32:56","slug":"java-streaming","status":"publish","type":"post","link":"https:\/\/liangqi.org\/?p=111","title":{"rendered":"Java Streaming Basic"},"content":{"rendered":"\n<p>10\u5e74\u524d\u52a0\u5165\u73b0\u5728\u7684\u516c\u53f8\u7684\u65f6\u5019\u516c\u53f8\u7684\u4ea7\u54c1\u8fd8\u5728\u7528JDK5\uff0c\u5199code\u81ea\u7136\u5b8c\u5168\u4e0d\u4f1a\u7528Lamda\uff0cStreaming\u8fd9\u4e9b8\u624d\u51fa\u73b0\u7684\u7279\u6027\u3002<\/p>\n\n\n\n<p>\u540e\u6765\u5f00\u59cb\u505amicro service\u4e86\uff0c\u7528\u5230\u65b0\u7248\u672c\u7684JDK\uff0c\u4f46\u662f\u6211\u4e5f\u5f88\u5c11\u5199code\u4e86\u3002\u4e2d\u95f4\u770b\u4e86\u4e00\u4e9b\u4e66\uff0c\u4e0d\u8fc7\u4e00\u76f4\u662f\u4f3c\u662f\u800c\u975e\u3002\u662f\u65f6\u95f4\u4ed4\u7ec6\u770b\u4e0b\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"intermediate-operations\">Intermediate Operations<\/h2>\n\n\n\n<p>filter<\/p>\n\n\n\n<p>limit<\/p>\n\n\n\n<p>map<\/p>\n\n\n\n<p>flatMap<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"stream-map\u548cstream-flatmap\u7684\u533a\u522b\">Stream.map\u548cStream.flatMap\u7684\u533a\u522b<\/h3>\n\n\n\n<p>\u5148\u770bsignature. \u533a\u522b\u662f flatMap\u7a7f\u8fdb\u53bb\u4e00\u4e2aItem\uff0c\u51fa\u6765\u4e00\u4e2aStream:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;R&gt; Stream&lt;R&gt; flatMap(Function&lt;? super T, ? extends Stream&lt;? extends R&gt;&gt; mapper);\n\n&lt;R&gt; Stream&lt;R&gt; map(Function&lt;? super T, ? extends R&gt; mapper);<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e24\u4e2a\u5176\u5b9e\u662f\u5b8c\u5168\u4e0d\u4e00\u6837\u7684\u4e1c\u897f\uff0cmap\u5c31\u662f\u628asteam\u91cc\u9762\u7684item\u8f6c\u6362\u6210\u53e6\u5916\u4e00\u4e2a\uff0c\u975e\u5e38\u7b80\u5355\u3002flatMap\u662f\u7528\u6765\u5904\u7406item\u4e5f\u662flist\u7684\u60c5\u51b5\uff0c\u6709\u65f6\u5019\u6211\u4eec\u60f3\u628a\u4e24\u7ef4\u5217\u8868\u8f6c\u5316\u6210\u4e00\u4e2a\uff0c\u5c31\u53ef\u4ee5\u7528\u8fd9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;List&lt;String&gt;&gt; list = Arrays.asList(\n  Arrays.asList(\"a\"),\n  Arrays.asList(\"b\"));\nSystem.out.println(list\n  .stream()\n  .flatMap(Collection::stream)\n  .collect(Collectors.toList()));\n\/\/https:\/\/www.baeldung.com\/java-difference-map-and-flatmap<\/code><\/pre>\n\n\n\n<p>\u4e0b\u9762\u662f\u9879\u76ee\u7684\u4e00\u4e2a\u66f4\u52a0\u5b9e\u9645\u7684\u4f8b\u5b50:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Set&lt;ObjectMetadataModel.FieldMapping&gt; fields = metadataStore.getAllObjectsForDataSourceType(object.getDataSourceObject())\n                .stream()\n                .map(ObjectMetadataModel::getFields)\n                .flatMap(Collection::stream)\n                .collect(toSet());<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u662f\u62ff\u5230\u4e00\u4e2a\u7236\u5bf9\u8c61\uff0c\u7236\u5bf9\u8c61\u91cc\u9762\u6709\u5b50\u5bf9\u8c61\uff0c\u8fd9\u6837\u5c31\u80fd\u62ff\u5230\u6240\u6709\u7684\u5b50\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u603b\u7684\u6765\u8bf4stream\u5c31\u662f\u4e00\u4e2a\u5faa\u73af\uff0c\u800cflatMap\u5c31\u662f\u4e00\u4e2a\u5d4c\u5957\u5faa\u73af\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u53e6\u5916\u4e00\u4e2alamda\u7684\u4f8b\u5b50\uff08\u4e0d\u662fmethod reference):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u53cc\u91cd\u5faa\u73af\nfor(Suit suit : Suit.values()){\n  for(Rank rank : Rank.values()){\n     result.add( new Card(suit, rank) );\n  }\n}\n\n\/\/\u5bf9\u5e94\u7684stream\nStream.of(Suit.values())\n  .flatMap( suit -&gt; Stream.of(Rank.values())\n                          .map( rank -&gt; new Card(suit.rank)))\n  .collect(toList()); \/\/ Collectors::toList<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"terminal-operation\">Terminal Operation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"foreach\">forEach<\/h3>\n\n\n\n<p>forE<em>ach\u5176\u5b9e\u5c31\u662fiterate.\u4e0d\u7528\u6539\u88ab\u7528\u4f5c\u8ba1\u7b97\uff0c\u5c31\u662f\u7528\u7b80\u5355\u7684report\uff0c\u6216\u8005\u7b80\u5355\u7684\u64cd\u4f5c\u6bd4\u5982\u628a\u7ed3\u679c\u52a0\u5230\u53e6\u4e00\u4e2acolection\u91cc\u9762. &#8211;&lt;Effective Java&gt;<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"collect\">collect <\/h3>\n\n\n\n<p>\u5e38\u7528\u7684\u7684\u4e0b\u9762\u51e0\u79cd:<\/p>\n\n\n\n<p>toList<\/p>\n\n\n\n<p>toSet<\/p>\n\n\n\n<p>toMap\uff1a\u5982\u679c\u6709\u91cd\u590dkey\uff0c\u4f1a\u51fa\u9519\u3002\u7b2c\u4e09\u4e2a\u53c2\u6570\u53ef\u4ee5\u7528\u6765\u53bb\u91cd\u3002\u6bd4\u5982\u53ef\u4ee5\u9009\u62e9\u6700\u5927\uff0c\u9009\u62e9\u6700\u540e\u7684\u4e00\u4e2a\u3002<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u9009\u62e9\u9500\u552e\u989d\u5927\u7684\ntoMap(keyMapper, valueMapper, maxBy(comparing(Album::sales)));\n\/\/\u9009\u62e9\u6700\u540e\u7684\u4e00\u4e2a\ntoMap(keyMapper, valueMapper, (v1,v2) -&gt; V2<\/code><\/pre>\n\n\n\n<p>groupingBy\uff1aValue\u53ef\u4ee5\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u662fsummary\uff0c\u6bd4\u5982`counting()`.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String&#91;] dd = {\"absw\",\"resa\",\"eras\",\"dds\",\"basw\",\"rr\",\"rr\"};\nArrays.stream(dd).collect(Collectors.groupingBy(e -&gt; e.substring(0,1)))\n                .forEach((key, value) -&gt; System.out.println(\"key:\" + key + \" value:\" + value));\n\/\/output\nkey:a value:&#91;absw]\nkey:b value:&#91;basw]\nkey:r value:&#91;resa, rr, rr]\nkey:d value:&#91;dds]\nkey:e value:&#91;eras]<\/code><\/pre>\n\n\n\n<p>Joining: \u4e09\u4e2a\u53c2\u6570\uff0cjoin\u7684delimiter, start character\uff0cend character<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String chString = str.stream()\n                .map(String::valueOf)\n                .collect(Collectors.joining(\", \", \"{\", \"}\"));\n\/\/Output:\n{Geeks, for, Geeks}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>10\u5e74\u524d\u52a0\u5165\u73b0\u5728\u7684\u516c\u53f8\u7684\u65f6\u5019\u516c\u53f8\u7684\u4ea7\u54c1\u8fd8\u5728\u7528JDK5\uff0c\u5199code\u81ea\u7136\u5b8c\u5168\u4e0d\u4f1a\u7528Lamda\uff0cStreaming\u8fd9\u4e9b8\u624d\u51fa\u73b0\u7684\u7279\u6027\u3002 \u540e\u6765\u5f00\u59cb\u505amicro service\u4e86\uff0c\u7528\u5230\u65b0\u7248\u672c\u7684JDK\uff0c\u4f46\u662f\u6211\u4e5f\u5f88\u5c11\u5199code\u4e86\u3002\u4e2d\u95f4\u770b\u4e86\u4e00\u4e9b\u4e66\uff0c\u4e0d\u8fc7\u4e00\u76f4\u662f\u4f3c\u662f\u800c\u975e\u3002\u662f\u65f6\u95f4\u4ed4\u7ec6\u770b\u4e0b\u4e86\u3002 Intermediate Operations filter limit map flatMap Stream.map\u548cStream.flatMap\u7684\u533a\u522b \u5148\u770bsignature. \u533a\u522b\u662f flatMap\u7a7f\u8fdb\u53bb\u4e00\u4e2aItem\uff0c\u51fa\u6765\u4e00\u4e2aStream: \u8fd9\u4e24\u4e2a\u5176\u5b9e\u662f\u5b8c\u5168\u4e0d\u4e00\u6837\u7684\u4e1c\u897f\uff0cmap\u5c31\u662f\u628asteam\u91cc\u9762\u7684item\u8f6c\u6362\u6210\u53e6\u5916\u4e00\u4e2a\uff0c\u975e\u5e38\u7b80\u5355\u3002flatMap\u662f\u7528\u6765\u5904\u7406item\u4e5f\u662flist\u7684\u60c5\u51b5\uff0c\u6709\u65f6\u5019\u6211\u4eec\u60f3\u628a\u4e24\u7ef4\u5217\u8868\u8f6c\u5316\u6210\u4e00\u4e2a\uff0c\u5c31\u53ef\u4ee5\u7528\u8fd9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a \u4e0b\u9762\u662f\u9879\u76ee\u7684\u4e00\u4e2a\u66f4\u52a0\u5b9e\u9645\u7684\u4f8b\u5b50: \u8fd9\u4e2a\u4f8b\u5b50\u662f\u62ff\u5230\u4e00\u4e2a\u7236\u5bf9\u8c61\uff0c\u7236\u5bf9\u8c61\u91cc\u9762\u6709\u5b50\u5bf9\u8c61\uff0c\u8fd9\u6837\u5c31\u80fd\u62ff\u5230\u6240\u6709\u7684\u5b50\u5bf9\u8c61\u3002 \u603b\u7684\u6765\u8bf4stream\u5c31\u662f\u4e00\u4e2a\u5faa\u73af\uff0c\u800cflatMap\u5c31\u662f\u4e00\u4e2a\u5d4c\u5957\u5faa\u73af\u3002 \u4e0b\u9762\u662f\u53e6\u5916\u4e00\u4e2alamda\u7684\u4f8b\u5b50\uff08\u4e0d\u662fmethod reference): Terminal Operation forEach forEach\u5176\u5b9e\u5c31\u662fiterate.\u4e0d\u7528\u6539\u88ab\u7528\u4f5c\u8ba1\u7b97\uff0c\u5c31\u662f\u7528\u7b80\u5355\u7684report\uff0c\u6216\u8005\u7b80\u5355\u7684\u64cd\u4f5c\u6bd4\u5982\u628a\u7ed3\u679c\u52a0\u5230\u53e6\u4e00\u4e2acolection\u91cc\u9762. &#8211;&lt;Effective Java&gt; collect \u5e38\u7528\u7684\u7684\u4e0b\u9762\u51e0\u79cd: toList toSet toMap\uff1a\u5982\u679c\u6709\u91cd\u590dkey\uff0c\u4f1a\u51fa\u9519\u3002\u7b2c\u4e09\u4e2a\u53c2\u6570\u53ef\u4ee5\u7528\u6765\u53bb\u91cd\u3002\u6bd4\u5982\u53ef\u4ee5\u9009\u62e9\u6700\u5927\uff0c\u9009\u62e9\u6700\u540e\u7684\u4e00\u4e2a\u3002 groupingBy\uff1aValue\u53ef\u4ee5\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u662fsummary\uff0c\u6bd4\u5982`counting()`. Joining: \u4e09\u4e2a\u53c2\u6570\uff0cjoin\u7684delimiter, start character\uff0cend character<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,16],"tags":[],"class_list":["post-111","post","type-post","status-publish","format-standard","hentry","category-java","category-16"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Streaming Basic - Liangqi\u2018s Technical Journey<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/liangqi.org\/?p=111\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Streaming Basic - Liangqi\u2018s Technical Journey\" \/>\n<meta property=\"og:description\" content=\"10\u5e74\u524d\u52a0\u5165\u73b0\u5728\u7684\u516c\u53f8\u7684\u65f6\u5019\u516c\u53f8\u7684\u4ea7\u54c1\u8fd8\u5728\u7528JDK5\uff0c\u5199code\u81ea\u7136\u5b8c\u5168\u4e0d\u4f1a\u7528Lamda\uff0cStreaming\u8fd9\u4e9b8\u624d\u51fa\u73b0\u7684\u7279\u6027\u3002 \u540e\u6765\u5f00\u59cb\u505amicro service\u4e86\uff0c\u7528\u5230\u65b0\u7248\u672c\u7684JDK\uff0c\u4f46\u662f\u6211\u4e5f\u5f88\u5c11\u5199code\u4e86\u3002\u4e2d\u95f4\u770b\u4e86\u4e00\u4e9b\u4e66\uff0c\u4e0d\u8fc7\u4e00\u76f4\u662f\u4f3c\u662f\u800c\u975e\u3002\u662f\u65f6\u95f4\u4ed4\u7ec6\u770b\u4e0b\u4e86\u3002 Intermediate Operations filter limit map flatMap Stream.map\u548cStream.flatMap\u7684\u533a\u522b \u5148\u770bsignature. \u533a\u522b\u662f flatMap\u7a7f\u8fdb\u53bb\u4e00\u4e2aItem\uff0c\u51fa\u6765\u4e00\u4e2aStream: \u8fd9\u4e24\u4e2a\u5176\u5b9e\u662f\u5b8c\u5168\u4e0d\u4e00\u6837\u7684\u4e1c\u897f\uff0cmap\u5c31\u662f\u628asteam\u91cc\u9762\u7684item\u8f6c\u6362\u6210\u53e6\u5916\u4e00\u4e2a\uff0c\u975e\u5e38\u7b80\u5355\u3002flatMap\u662f\u7528\u6765\u5904\u7406item\u4e5f\u662flist\u7684\u60c5\u51b5\uff0c\u6709\u65f6\u5019\u6211\u4eec\u60f3\u628a\u4e24\u7ef4\u5217\u8868\u8f6c\u5316\u6210\u4e00\u4e2a\uff0c\u5c31\u53ef\u4ee5\u7528\u8fd9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a \u4e0b\u9762\u662f\u9879\u76ee\u7684\u4e00\u4e2a\u66f4\u52a0\u5b9e\u9645\u7684\u4f8b\u5b50: \u8fd9\u4e2a\u4f8b\u5b50\u662f\u62ff\u5230\u4e00\u4e2a\u7236\u5bf9\u8c61\uff0c\u7236\u5bf9\u8c61\u91cc\u9762\u6709\u5b50\u5bf9\u8c61\uff0c\u8fd9\u6837\u5c31\u80fd\u62ff\u5230\u6240\u6709\u7684\u5b50\u5bf9\u8c61\u3002 \u603b\u7684\u6765\u8bf4stream\u5c31\u662f\u4e00\u4e2a\u5faa\u73af\uff0c\u800cflatMap\u5c31\u662f\u4e00\u4e2a\u5d4c\u5957\u5faa\u73af\u3002 \u4e0b\u9762\u662f\u53e6\u5916\u4e00\u4e2alamda\u7684\u4f8b\u5b50\uff08\u4e0d\u662fmethod reference): Terminal Operation forEach forEach\u5176\u5b9e\u5c31\u662fiterate.\u4e0d\u7528\u6539\u88ab\u7528\u4f5c\u8ba1\u7b97\uff0c\u5c31\u662f\u7528\u7b80\u5355\u7684report\uff0c\u6216\u8005\u7b80\u5355\u7684\u64cd\u4f5c\u6bd4\u5982\u628a\u7ed3\u679c\u52a0\u5230\u53e6\u4e00\u4e2acolection\u91cc\u9762. &#8211;&lt;Effective Java&gt; collect \u5e38\u7528\u7684\u7684\u4e0b\u9762\u51e0\u79cd: toList toSet toMap\uff1a\u5982\u679c\u6709\u91cd\u590dkey\uff0c\u4f1a\u51fa\u9519\u3002\u7b2c\u4e09\u4e2a\u53c2\u6570\u53ef\u4ee5\u7528\u6765\u53bb\u91cd\u3002\u6bd4\u5982\u53ef\u4ee5\u9009\u62e9\u6700\u5927\uff0c\u9009\u62e9\u6700\u540e\u7684\u4e00\u4e2a\u3002 groupingBy\uff1aValue\u53ef\u4ee5\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u662fsummary\uff0c\u6bd4\u5982`counting()`. Joining: \u4e09\u4e2a\u53c2\u6570\uff0cjoin\u7684delimiter, start character\uff0cend character\" \/>\n<meta property=\"og:url\" content=\"https:\/\/liangqi.org\/?p=111\" \/>\n<meta property=\"og:site_name\" content=\"Liangqi\u2018s Technical Journey\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-02T19:51:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-02-02T22:32:56+00:00\" \/>\n<meta name=\"author\" content=\"liangqi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"liangqi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/liangqi.org\/?p=111#article\",\"isPartOf\":{\"@id\":\"https:\/\/liangqi.org\/?p=111\"},\"author\":{\"name\":\"liangqi\",\"@id\":\"https:\/\/liangqi.org\/#\/schema\/person\/105c89d9b783fda67b62e3ce113d6cd3\"},\"headline\":\"Java Streaming Basic\",\"datePublished\":\"2022-02-02T19:51:31+00:00\",\"dateModified\":\"2022-02-02T22:32:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/liangqi.org\/?p=111\"},\"wordCount\":64,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/liangqi.org\/#\/schema\/person\/105c89d9b783fda67b62e3ce113d6cd3\"},\"articleSection\":[\"Java\",\"\u6280\u672f\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/liangqi.org\/?p=111#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/liangqi.org\/?p=111\",\"url\":\"https:\/\/liangqi.org\/?p=111\",\"name\":\"Java Streaming Basic - Liangqi\u2018s Technical Journey\",\"isPartOf\":{\"@id\":\"https:\/\/liangqi.org\/#website\"},\"datePublished\":\"2022-02-02T19:51:31+00:00\",\"dateModified\":\"2022-02-02T22:32:56+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/liangqi.org\/?p=111#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/liangqi.org\/?p=111\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/liangqi.org\/?p=111#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/liangqi.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Streaming Basic\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/liangqi.org\/#website\",\"url\":\"https:\/\/liangqi.org\/\",\"name\":\"Liangqi\u2018s Technical Journey\",\"description\":\"Chasing Excellence; Enjoy life.\",\"publisher\":{\"@id\":\"https:\/\/liangqi.org\/#\/schema\/person\/105c89d9b783fda67b62e3ce113d6cd3\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/liangqi.org\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/liangqi.org\/#\/schema\/person\/105c89d9b783fda67b62e3ce113d6cd3\",\"name\":\"liangqi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/liangqi.org\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/liangqi.org\/wp-content\/uploads\/2022\/01\/P1100089-3-scaled.jpg\",\"contentUrl\":\"https:\/\/liangqi.org\/wp-content\/uploads\/2022\/01\/P1100089-3-scaled.jpg\",\"width\":2560,\"height\":1920,\"caption\":\"liangqi\"},\"logo\":{\"@id\":\"https:\/\/liangqi.org\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/liangqi.org\"],\"url\":\"https:\/\/liangqi.org\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Streaming Basic - Liangqi\u2018s Technical Journey","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/liangqi.org\/?p=111","og_locale":"en_US","og_type":"article","og_title":"Java Streaming Basic - Liangqi\u2018s Technical Journey","og_description":"10\u5e74\u524d\u52a0\u5165\u73b0\u5728\u7684\u516c\u53f8\u7684\u65f6\u5019\u516c\u53f8\u7684\u4ea7\u54c1\u8fd8\u5728\u7528JDK5\uff0c\u5199code\u81ea\u7136\u5b8c\u5168\u4e0d\u4f1a\u7528Lamda\uff0cStreaming\u8fd9\u4e9b8\u624d\u51fa\u73b0\u7684\u7279\u6027\u3002 \u540e\u6765\u5f00\u59cb\u505amicro service\u4e86\uff0c\u7528\u5230\u65b0\u7248\u672c\u7684JDK\uff0c\u4f46\u662f\u6211\u4e5f\u5f88\u5c11\u5199code\u4e86\u3002\u4e2d\u95f4\u770b\u4e86\u4e00\u4e9b\u4e66\uff0c\u4e0d\u8fc7\u4e00\u76f4\u662f\u4f3c\u662f\u800c\u975e\u3002\u662f\u65f6\u95f4\u4ed4\u7ec6\u770b\u4e0b\u4e86\u3002 Intermediate Operations filter limit map flatMap Stream.map\u548cStream.flatMap\u7684\u533a\u522b \u5148\u770bsignature. \u533a\u522b\u662f flatMap\u7a7f\u8fdb\u53bb\u4e00\u4e2aItem\uff0c\u51fa\u6765\u4e00\u4e2aStream: \u8fd9\u4e24\u4e2a\u5176\u5b9e\u662f\u5b8c\u5168\u4e0d\u4e00\u6837\u7684\u4e1c\u897f\uff0cmap\u5c31\u662f\u628asteam\u91cc\u9762\u7684item\u8f6c\u6362\u6210\u53e6\u5916\u4e00\u4e2a\uff0c\u975e\u5e38\u7b80\u5355\u3002flatMap\u662f\u7528\u6765\u5904\u7406item\u4e5f\u662flist\u7684\u60c5\u51b5\uff0c\u6709\u65f6\u5019\u6211\u4eec\u60f3\u628a\u4e24\u7ef4\u5217\u8868\u8f6c\u5316\u6210\u4e00\u4e2a\uff0c\u5c31\u53ef\u4ee5\u7528\u8fd9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a \u4e0b\u9762\u662f\u9879\u76ee\u7684\u4e00\u4e2a\u66f4\u52a0\u5b9e\u9645\u7684\u4f8b\u5b50: \u8fd9\u4e2a\u4f8b\u5b50\u662f\u62ff\u5230\u4e00\u4e2a\u7236\u5bf9\u8c61\uff0c\u7236\u5bf9\u8c61\u91cc\u9762\u6709\u5b50\u5bf9\u8c61\uff0c\u8fd9\u6837\u5c31\u80fd\u62ff\u5230\u6240\u6709\u7684\u5b50\u5bf9\u8c61\u3002 \u603b\u7684\u6765\u8bf4stream\u5c31\u662f\u4e00\u4e2a\u5faa\u73af\uff0c\u800cflatMap\u5c31\u662f\u4e00\u4e2a\u5d4c\u5957\u5faa\u73af\u3002 \u4e0b\u9762\u662f\u53e6\u5916\u4e00\u4e2alamda\u7684\u4f8b\u5b50\uff08\u4e0d\u662fmethod reference): Terminal Operation forEach forEach\u5176\u5b9e\u5c31\u662fiterate.\u4e0d\u7528\u6539\u88ab\u7528\u4f5c\u8ba1\u7b97\uff0c\u5c31\u662f\u7528\u7b80\u5355\u7684report\uff0c\u6216\u8005\u7b80\u5355\u7684\u64cd\u4f5c\u6bd4\u5982\u628a\u7ed3\u679c\u52a0\u5230\u53e6\u4e00\u4e2acolection\u91cc\u9762. &#8211;&lt;Effective Java&gt; collect \u5e38\u7528\u7684\u7684\u4e0b\u9762\u51e0\u79cd: toList toSet toMap\uff1a\u5982\u679c\u6709\u91cd\u590dkey\uff0c\u4f1a\u51fa\u9519\u3002\u7b2c\u4e09\u4e2a\u53c2\u6570\u53ef\u4ee5\u7528\u6765\u53bb\u91cd\u3002\u6bd4\u5982\u53ef\u4ee5\u9009\u62e9\u6700\u5927\uff0c\u9009\u62e9\u6700\u540e\u7684\u4e00\u4e2a\u3002 groupingBy\uff1aValue\u53ef\u4ee5\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u662fsummary\uff0c\u6bd4\u5982`counting()`. Joining: \u4e09\u4e2a\u53c2\u6570\uff0cjoin\u7684delimiter, start character\uff0cend character","og_url":"https:\/\/liangqi.org\/?p=111","og_site_name":"Liangqi\u2018s Technical Journey","article_published_time":"2022-02-02T19:51:31+00:00","article_modified_time":"2022-02-02T22:32:56+00:00","author":"liangqi","twitter_card":"summary_large_image","twitter_misc":{"Written by":"liangqi","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/liangqi.org\/?p=111#article","isPartOf":{"@id":"https:\/\/liangqi.org\/?p=111"},"author":{"name":"liangqi","@id":"https:\/\/liangqi.org\/#\/schema\/person\/105c89d9b783fda67b62e3ce113d6cd3"},"headline":"Java Streaming Basic","datePublished":"2022-02-02T19:51:31+00:00","dateModified":"2022-02-02T22:32:56+00:00","mainEntityOfPage":{"@id":"https:\/\/liangqi.org\/?p=111"},"wordCount":64,"commentCount":0,"publisher":{"@id":"https:\/\/liangqi.org\/#\/schema\/person\/105c89d9b783fda67b62e3ce113d6cd3"},"articleSection":["Java","\u6280\u672f"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/liangqi.org\/?p=111#respond"]}]},{"@type":"WebPage","@id":"https:\/\/liangqi.org\/?p=111","url":"https:\/\/liangqi.org\/?p=111","name":"Java Streaming Basic - Liangqi\u2018s Technical Journey","isPartOf":{"@id":"https:\/\/liangqi.org\/#website"},"datePublished":"2022-02-02T19:51:31+00:00","dateModified":"2022-02-02T22:32:56+00:00","breadcrumb":{"@id":"https:\/\/liangqi.org\/?p=111#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/liangqi.org\/?p=111"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/liangqi.org\/?p=111#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/liangqi.org\/"},{"@type":"ListItem","position":2,"name":"Java Streaming Basic"}]},{"@type":"WebSite","@id":"https:\/\/liangqi.org\/#website","url":"https:\/\/liangqi.org\/","name":"Liangqi\u2018s Technical Journey","description":"Chasing Excellence; Enjoy life.","publisher":{"@id":"https:\/\/liangqi.org\/#\/schema\/person\/105c89d9b783fda67b62e3ce113d6cd3"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/liangqi.org\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/liangqi.org\/#\/schema\/person\/105c89d9b783fda67b62e3ce113d6cd3","name":"liangqi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/liangqi.org\/#\/schema\/person\/image\/","url":"https:\/\/liangqi.org\/wp-content\/uploads\/2022\/01\/P1100089-3-scaled.jpg","contentUrl":"https:\/\/liangqi.org\/wp-content\/uploads\/2022\/01\/P1100089-3-scaled.jpg","width":2560,"height":1920,"caption":"liangqi"},"logo":{"@id":"https:\/\/liangqi.org\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/liangqi.org"],"url":"https:\/\/liangqi.org\/?author=1"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/liangqi.org\/index.php?rest_route=\/wp\/v2\/posts\/111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liangqi.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liangqi.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liangqi.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/liangqi.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=111"}],"version-history":[{"count":9,"href":"https:\/\/liangqi.org\/index.php?rest_route=\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":130,"href":"https:\/\/liangqi.org\/index.php?rest_route=\/wp\/v2\/posts\/111\/revisions\/130"}],"wp:attachment":[{"href":"https:\/\/liangqi.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liangqi.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/liangqi.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}