{"id":202833,"date":"2025-05-22T10:36:43","date_gmt":"2025-05-22T02:36:43","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202833\/"},"modified":"2025-05-22T10:36:43","modified_gmt":"2025-05-22T02:36:43","slug":"%e6%b7%b1%e5%85%a5%e5%89%96%e6%9e%90java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e5%9c%a8%e6%95%b0%e6%8d%ae%e5%a4%84%e7%90%86%e4%b8%ad%e7%9a%84%e4%bc%98%e5%8a%bf","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202833\/","title":{"rendered":"\u6df1\u5165\u5256\u6790Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5728\u6570\u636e\u5904\u7406\u4e2d\u7684\u4f18\u52bf"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u6df1\u5165\u5256\u6790Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5728\u6570\u636e\u5904\u7406\u4e2d\u7684\u4f18\u52bf<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u6df1\u5165\u5256\u6790Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5728\u6570\u636e\u5904\u7406\u4e2d\u7684\u4f18\u52bf\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u5728\u6570\u636e\u5904\u7406\u4e2d\u7684\u4f18\u52bf\u4e3b\u8981\u6709\uff1a\u4ee3\u7801\u7b80\u6d01\u6027\uff1a\u4f7f\u7528\u6d41 API \u8fdb\u884c\u6570\u636e\u64cd\u4f5c\uff0c\u4ee3\u7801\u7b80\u6d01\u6613\u8bfb\u3002\u6570\u636e\u4e0d\u53ef\u53d8\u6027\uff1a\u901a\u8fc7\u4f7f\u7528\u4e0d\u53ef\u53d8\u96c6\u5408\u7c7b\uff0c\u907f\u514d\u5e76\u53d1\u95ee\u9898\u548c\u6570\u636e\u7ade\u4e89\u3002\u9ad8\u6027\u80fd\uff1a\u51fd\u6570\u5f0f\u64cd\u4f5c\u4ee5\u4e0d\u53ef\u53d8\u65b9\u5f0f\u5e76\u884c\u6267\u884c\uff0c\u63d0\u9ad8\u5927\u6570\u636e\u96c6\u5904\u7406\u6548\u7387\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729948550671ceb8604ab7.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u6df1\u5165\u5256\u6790 Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u5728\u6570\u636e\u5904\u7406\u4e2d\u7684\u4f18\u52bf<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u5f3a\u8c03\u4f7f\u7528\u4e0d\u53ef\u53d8\u6570\u636e\u3001\u7eaf\u51fd\u6570\u548c\u9ad8\u9636\u51fd\u6570\u3002\u5728\u6570\u636e\u5904\u7406\u9886\u57df\uff0c\u51fd\u6570\u5f0f\u7f16\u7a0b\u5e26\u6765\u4e86\u8bf8\u591a\u4f18\u52bf\uff0c\u5305\u62ec\uff1a<\/p>\n<p><strong>\u4ee3\u7801\u7b80\u6d01\u6027<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u9f13\u52b1\u4f7f\u7528\u7b80\u6d01\u800c\u5bcc\u6709\u8868\u73b0\u529b\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u8fd9\u6bb5 Java \u4ee3\u7801\u4f7f\u7528\u6d41 API \u6765\u8fc7\u6ee4\u548c\u6620\u5c04\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<pre>List&lt;Integer&gt; numbers = List.of(1, 2, 3, 4, 5);\nList&lt;Integer&gt; evenNumbers = numbers.stream()\n                                    .filter(n -&gt; n % 2 == 0)\n                                    .map(n -&gt; n * 2)\n                                    .toList();<\/pre>\n<p><strong>\u6570\u636e\u4e0d\u53ef\u53d8\u6027<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u5f3a\u8c03\u6570\u636e\u4e0d\u53ef\u53d8\u6027\uff0c\u8fd9\u6709\u52a9\u4e8e\u907f\u514d\u5e76\u53d1\u95ee\u9898\u548c\u6570\u636e\u7ade\u4e89\u3002\u901a\u8fc7\u4f7f\u7528\u4e0d\u53ef\u53d8\u96c6\u5408\u7c7b\uff0c\u4f8b\u5982 <code>List<\/code> \u548c <code>Set<\/code>\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5728\u6574\u4e2a\u5904\u7406\u8fc7\u7a0b\u4e2d\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<p><strong>\u9ad8\u6027\u80fd<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u64cd\u4f5c\u666e\u904d\u4ee5\u4e0d\u53ef\u53d8\u65b9\u5f0f\u5e76\u884c\u6267\u884c\uff0c\u65e0\u9700\u6d89\u53ca\u5171\u4eab\u72b6\u6001\u3002\u8fd9\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5e76\u884c\u573a\u666f\u4e2d\u6570\u636e\u5904\u7406\u7684\u6027\u80fd\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u4f7f\u7528 Java \u6d41 API \u5904\u7406\u5927\u6570\u636e\u96c6<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u5b9e\u6218\u6848\u4f8b\uff1a\u6211\u4eec\u9700\u8981\u5904\u7406\u4e00\u4e2a\u5305\u542b\u6570\u767e\u4e07\u8bb0\u5f55\u7684\u5927\u6570\u636e\u96c6\uff0c\u5e76\u627e\u5230\u6570\u636e\u96c6\u4e2d\u7684\u6240\u6709\u5355\u8bcd\u3002<\/p>\n<p><strong>\u4f20\u7edf\u65b9\u6cd5<\/strong><\/p>\n<p>\u4f7f\u7528\u4f20\u7edf\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u6570\u636e\u96c6\uff0c\u5c06\u6bcf\u4e2a\u5355\u8bcd\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4f7f\u7528 <code>Collections.sort()<\/code> \u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u8fd9\u53ef\u80fd\u4f1a\u5360\u7528\u5927\u91cf\u5185\u5b58\uff0c\u5e76\u4e14\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u4f1a\u975e\u5e38\u6162\u3002<\/p>\n<p><strong>\u51fd\u6570\u5f0f\u65b9\u6cd5<\/strong><\/p>\n<p>\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6d41 API \u4ee5\u66f4\u6709\u6548\u7684\u65b9\u5f0f\u6267\u884c\u76f8\u540c\u7684\u4efb\u52a1\uff1a<\/p>\n<pre>Path path = Paths.get(\"data.txt\");\ntry (Stream&lt;String&gt; lines = Files.lines(path)) {\n    Map&lt;String, Long&gt; wordCounts = lines.flatMap(line -&gt; Arrays.stream(line.split(\" \")))\n                                         .filter(word -&gt; !word.isBlank())\n                                         .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));\n}<\/pre>\n<p>\u6b64\u4ee3\u7801\u7247\u6bb5\u5c06\u6d41\u8f6c\u6362\u4e3a\u5355\u8bcd\u6d41\uff0c\u8fc7\u6ee4\u6389\u7a7a\u767d\u5355\u8bcd\uff0c\u7136\u540e\u5c06\u5176\u5206\u7ec4\u5e76\u6839\u636e\u51fa\u73b0\u7684\u6b21\u6570\u6392\u5e8f\u3002\u5b83\u907f\u514d\u4e86\u521b\u5efa\u5927\u578b\u5217\u8868\u5e76\u4f7f\u7528\u4e0d\u53ef\u53d8\u65b9\u5f0f\u5e76\u884c\u6267\u884c\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u5728 Java \u6570\u636e\u5904\u7406\u4e2d\u5177\u6709\u663e\u7740\u7684\u4f18\u52bf\u3002\u5b83\u63d0\u4f9b\u7b80\u6d01\u7684\u4ee3\u7801\u3001\u6570\u636e\u4e0d\u53ef\u53d8\u6027\u548c\u9ad8\u6027\u80fd\uff0c\u4f7f\u5904\u7406\u5927\u6570\u636e\u96c6\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u548c\u53ef\u9760\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u6df1\u5165\u5256\u6790Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5728\u6570\u636e\u5904\u7406\u4e2d\u7684\u4f18\u52bf\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u5256\u6790Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5728\u6570\u636e&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202833","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202833"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202833\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}