{"id":203088,"date":"2025-05-22T08:26:42","date_gmt":"2025-05-22T00:26:42","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203088\/"},"modified":"2025-05-22T08:26:42","modified_gmt":"2025-05-22T00:26:42","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e4%b8%ad%e9%ab%98%e9%98%b6%e5%87%bd%e6%95%b0%e4%b8%8e%e5%8c%bf%e5%90%8d%e5%86%85%e9%83%a8%e7%b1%bb%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203088\/","title":{"rendered":"Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u4e0e\u533f\u540d\u5185\u90e8\u7c7b\u6709\u4ec0\u4e48\u533a\u522b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u4e0e\u533f\u540d\u5185\u90e8\u7c7b\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/h1>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u4e0e\u533f\u540d\u5185\u90e8\u7c7b\u6709\u4ec0\u4e48\u533a\u522b\uff1f\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p>\u9ad8\u9636\u51fd\u6570\u548c\u533f\u540d\u5185\u90e8\u7c7b\u5728 Java \u4e2d\u90fd\u7528\u4e8e\u5904\u7406\u51fd\u6570\uff0c\u4f46\u5b58\u5728\u4ee5\u4e0b\u533a\u522b\uff1a\u8bed\u6cd5\uff1a\u9ad8\u9636\u51fd\u6570\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\uff0c\u800c\u533f\u540d\u5185\u90e8\u7c7b\u4f7f\u7528\u7c7b\u5b57\u9762\u91cf\u3002\u76ee\u7684\uff1a\u9ad8\u9636\u51fd\u6570\u7528\u4e8e\u51fd\u6570\u5f0f\u7f16\u7a0b\u548c\u64cd\u4f5c\u5176\u4ed6\u51fd\u6570\uff0c\u533f\u540d\u5185\u90e8\u7c7b\u7528\u4e8e\u5b9e\u73b0\u63a5\u53e3\u6216\u62bd\u8c61\u7c7b\u3002\u7528\u6cd5\uff1a\u9ad8\u9636\u51fd\u6570\u7528\u4f5c\u51fd\u6570\u5f0f\u63a5\u53e3\u7684\u5b9e\u73b0\uff0c\u533f\u540d\u5185\u90e8\u7c7b\u7528\u4e8e\u533f\u540d\u5b9e\u73b0\u7279\u5b9a\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730080174671eedae4c93c.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u4e0e\u533f\u540d\u5185\u90e8\u7c7b\u4e4b\u95f4\u7684\u533a\u522b<\/strong><\/p>\n<p>\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u9ad8\u9636\u51fd\u6570\u548c\u533f\u540d\u5185\u90e8\u7c7b\u90fd\u662f\u7528\u4e8e\u64cd\u4f5c\u5176\u4ed6\u51fd\u6570\u7684\u6784\u9020\u3002\u7136\u800c\uff0c\u5b83\u4eec\u5728\u8bed\u6cd5\u3001\u76ee\u7684\u548c\u7528\u6cd5\u4e0a\u5b58\u5728\u4e00\u4e9b\u5173\u952e\u533a\u522b\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u9ad8\u9636\u51fd\u6570:<\/strong><\/p>\n<pre>Function&lt;Integer, String&gt; function = (x) -&gt; \"Value is \" + x;<\/pre>\n<\/li>\n<li>\n<p><strong>\u533f\u540d\u5185\u90e8\u7c7b:<\/strong><\/p>\n<pre>Comparator&lt;Integer&gt; comparator = new Comparator&lt;Integer&gt;() {\n  @Override\n  public int compare(Integer a, Integer b) {\n      return a - b;\n  }\n};<\/pre>\n<\/li>\n<\/ul>\n<p><strong>\u76ee\u7684<\/strong><\/p>\n<ul>\n<li><strong>\u9ad8\u9636\u51fd\u6570:<\/strong> \u9ad8\u9636\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u6216\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u3002\u5b83\u4eec\u4e3b\u8981\u7528\u4e8e\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u7f16\u5199\u66f4\u7b80\u6d01\u3001\u53ef\u8bfb\u6027\u66f4\u5f3a\u7684\u4ee3\u7801\u3002<\/li>\n<li><strong>\u533f\u540d\u5185\u90e8\u7c7b:<\/strong> \u533f\u540d\u5185\u90e8\u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u5728\u4e0d\u521b\u5efa\u547d\u540d\u7c7b\u7684\u540c\u65f6\u5b9e\u73b0\u63a5\u53e3\u6216\u62bd\u8c61\u7c7b\u7684\u65b9\u6cd5\u3002\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u4e8b\u4ef6\u5904\u7406\u548c\u56de\u8c03\u3002<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u9ad8\u9636\u51fd\u6570:<\/strong> \u7528\u4f5c\u51fd\u6570\u5f0f\u63a5\u53e3\u7684\u5b9e\u73b0\uff0c\u5982:<\/p>\n<pre>List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3);\nnumbers.forEach(function); \/\/ \u4f7f\u7528 high order function \u5bf9\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u51fd\u6570<\/pre>\n<\/li>\n<li>\n<p><strong>\u533f\u540d\u5185\u90e8\u7c7b:<\/strong> \u4f5c\u4e3a\u7279\u5b9a\u7c7b\u578b\u5bf9\u8c61\u7684\u533f\u540d\u5b9e\u73b0\uff1a<\/p>\n<pre>JButton button = new JButton(\"Click me\");\nbutton.addActionListener(new ActionListener() {\n  @Override\n  public void actionPerformed(ActionEvent e) {\n      System.out.println(\"Button clicked!\");\n  }\n}<\/pre>\n<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u9700\u8981\u5c06\u4e00\u7ec4\u6570\u5b57\u6392\u5e8f\u5e76\u627e\u51fa\u6700\u5927\u503c\u7684\u573a\u666f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9ad8\u9636\u51fd\u6570\u548c\u533f\u540d\u5185\u90e8\u7c7b\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<p><strong>\u4f7f\u7528\u9ad8\u9636\u51fd\u6570:<\/strong><\/p>\n<pre>Function&lt;Integer, Integer&gt; absFunction = Math::abs;\nComparator&lt;Integer&gt; comparator = (a, b) -&gt; absFunction.apply(b) - absFunction.apply(a);\nint max = Collections.max(numbers, comparator);<\/pre>\n<p><strong>\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b:<\/strong><\/p>\n<pre>Comparator&lt;Integer&gt; comparator = new Comparator&lt;Integer&gt;() {\n    @Override\n    public int compare(Integer a, Integer b) {\n        return Math.abs(b) - Math.abs(a);\n    }\n};\nint max = Collections.max(numbers, comparator);<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u9ad8\u9636\u51fd\u6570\u548c\u533f\u540d\u5185\u90e8\u7c7b\u90fd\u662f\u5904\u7406\u51fd\u6570\u7684\u6709\u6548\u673a\u5236\uff0c\u4f46\u5b83\u4eec\u5728\u8bed\u6cd5\u3001\u76ee\u7684\u548c\u7528\u6cd5\u4e0a\u6709\u6240\u4e0d\u540c\u3002\u9ad8\u9636\u51fd\u6570\u66f4\u9002\u7528\u4e8e\u51fd\u6570\u5f0f\u7f16\u7a0b\uff0c\u800c\u533f\u540d\u5185\u90e8\u7c7b\u66f4\u9002\u7528\u4e8e\u5b9e\u73b0\u63a5\u53e3\u6216\u6267\u884c\u56de\u8c03\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u4e0e\u533f\u540d\u5185\u90e8\u7c7b\u6709\u4ec0\u4e48\u533a\u522b\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u4e0e\u533f&#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-203088","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203088","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=203088"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203088\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}