{"id":202525,"date":"2025-05-22T11:05:37","date_gmt":"2025-05-22T03:05:37","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202525\/"},"modified":"2025-05-22T11:05:37","modified_gmt":"2025-05-22T03:05:37","slug":"java-%e4%b8%ad%e9%ab%98%e9%98%b6%e5%87%bd%e6%95%b0%e5%92%8c%e5%af%b9%e8%b1%a1%e6%af%94%e8%be%83","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202525\/","title":{"rendered":"Java \u4e2d\u9ad8\u9636\u51fd\u6570\u548c\u5bf9\u8c61\u6bd4\u8f83"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java \u4e2d\u9ad8\u9636\u51fd\u6570\u548c\u5bf9\u8c61\u6bd4\u8f83<\/h1>\n<p>\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava \u4e2d\u9ad8\u9636\u51fd\u6570\u548c\u5bf9\u8c61\u6bd4\u8f83\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p>\u9ad8\u9636\u51fd\u6570\u63a5\u53d7\u6216\u8fd4\u56de\u51fd\u6570\uff0cJava 8 \u4ee5\u4e0a\u7248\u672c\u63d0\u4f9b\uff0c\u53ef\u5b9e\u73b0\u590d\u6742\u8f6c\u6362\u548c\u805a\u5408\u3002\u6b64\u5916\uff0cJava \u5177\u6709\u4e30\u5bcc\u7684\u5bf9\u8c61\u6bd4\u8f83\u673a\u5236\uff0c\u5305\u62ec equals() \u65b9\u6cd5\u3001\u53ef\u6bd4\u8f83\u63a5\u53e3\u548c Comparator \u63a5\u53e3\uff0c\u53ef\u6bd4\u8f83\u590d\u6742\u5bf9\u8c61\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729914094671c64ee2b736.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u4e2d\u7684\u9ad8\u9636\u51fd\u6570\u548c\u5bf9\u8c61\u6bd4\u8f83<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u9ad8\u9636\u51fd\u6570\u662f\u53ef\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u6216\u8fd4\u56de\u51fd\u6570\u7684\u51fd\u6570\u3002\u5b83\u5728 Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\u53ef\u7528\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7f16\u7a0b\u80fd\u529b\u3002\u6b64\u5916\uff0cJava \u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5bf9\u8c61\u6bd4\u8f83\u673a\u5236\uff0c\u53ef\u7528\u4e8e\u6bd4\u8f83\u590d\u6742\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u9ad8\u9636\u51fd\u6570<\/strong><\/p>\n<p>Java \u4e2d\u6709\u8bb8\u591a\u5185\u7f6e\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u5982 <code>map()<\/code>\u3001<code>filter()<\/code> \u548c <code>reduce()<\/code>\u3002\u8fd9\u4e9b\u51fd\u6570\u5141\u8bb8\u5728\u96c6\u5408\u5143\u7d20\u4e0a\u6267\u884c\u590d\u6742\u7684\u8f6c\u6362\u548c\u805a\u5408\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>map()<\/code><\/strong><\/p>\n<pre>List&lt;Integer&gt; numbers = List.of(1, 2, 3, 4, 5);\nList&lt;Integer&gt; doubledNumbers = numbers.stream()\n    .map(n -&gt; n * 2)\n    .toList();<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>map()<\/code> \u51fd\u6570\u5c06 numbers \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6620\u5c04\u5230\u5176\u4e24\u500d\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6620\u5c04\u540e\u7684\u5143\u7d20\u7684\u65b0\u5217\u8868\u3002<\/p>\n<p><strong>\u5bf9\u8c61\u6bd4\u8f83<\/strong><\/p>\n<p>Java \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6bd4\u8f83\u5bf9\u8c61\uff0c\u5305\u62ec <code>equals()<\/code> \u65b9\u6cd5\u3001\u53ef\u6bd4\u8f83\u63a5\u53e3\u548c <code>Comparator<\/code> \u63a5\u53e3\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>equals()<\/code><\/strong><\/p>\n<pre>Person person1 = new Person(\"John\", 30);\nPerson person2 = new Person(\"John\", 30);\n\nboolean areEqual = person1.equals(person2);<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>equals()<\/code> \u65b9\u6cd5\u88ab\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a <code>Person<\/code> \u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u7684\u5185\u5bb9\u76f8\u540c\uff0c\u5219\u8fd4\u56de <code>true<\/code>\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u4f7f\u7528\u9ad8\u9636\u51fd\u6570\u8fc7\u6ee4\u7528\u6237<\/strong><\/p>\n<pre>List&lt;User&gt; users = List.of(\n    new User(\"Alice\", 25),\n    new User(\"Bob\", 30),\n    new User(\"Carol\", 20)\n);\n\nList&lt;User&gt; youngUsers = users.stream()\n    .filter(u -&gt; u.getAge() &lt; 30)\n    .toList();<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>filter()<\/code> \u51fd\u6570\u88ab\u7528\u4e8e\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u7528\u6237\u5e74\u9f84\u5c0f\u4e8e 30 \u5c81\u7684\u7528\u6237\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5bf9\u8c61\u6bd4\u8f83\u6392\u5e8f\u5546\u54c1<\/strong><\/p>\n<pre>List&lt;Product&gt; products = List.of(\n    new Product(\"Product A\", 100),\n    new Product(\"Product B\", 200),\n    new Product(\"Product C\", 150)\n);\n\nproducts.sort(Comparator.comparing(Product::getPrice));<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>Comparator.comparing()<\/code> \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u6bd4\u8f83\u5668\uff0c\u8be5\u6bd4\u8f83\u5668\u6839\u636e\u4ea7\u54c1\u7684\u4ef7\u683c\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aJava \u4e2d\u9ad8\u9636\u51fd\u6570\u548c\u5bf9\u8c61\u6bd4\u8f83\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u4e2d\u9ad8\u9636\u51fd\u6570\u548c\u5bf9\u8c61\u6bd4\u8f83 &#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-202525","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202525","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=202525"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202525\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}