{"id":66938,"date":"2025-05-03T15:31:04","date_gmt":"2025-05-03T07:31:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66938\/"},"modified":"2025-05-03T15:31:04","modified_gmt":"2025-05-03T07:31:04","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e6%b1%82%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e6%9c%80%e5%a4%a7%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66938\/","title":{"rendered":"java\u4e2d\u600e\u4e48\u6c42\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c"},"content":{"rendered":"<blockquote><p>\n  \u6709 4 \u79cd\u65b9\u6cd5\u6c42 java \u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\uff1a\u4f7f\u7528 arrays.sort()\u4f7f\u7528\u5faa\u73af\u4f7f\u7528 stream api\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111719452435729.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u600e\u4e48\u6c42\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\u63d2\u56fe\" alt=\"java\u4e2d\u600e\u4e48\u6c42\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u6c42 Java \u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\uff1f<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6c42\u51fa\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Arrays.sort()<\/strong><\/p>\n<p>Arrays.sort() \u65b9\u6cd5\u6309\u7167\u5347\u5e8f\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u5927\u503c\u5c06\u4f4d\u4e8e\u6570\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] myArray = {1, 5, 3, 9, 7};\nArrays.sort(myArray);\nint maxValue = myArray[myArray.length - 1];\nSystem.out.println(\"\u6700\u5927\u503c\uff1a\" + maxValue);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u5faa\u73af<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u8bb0\u5f55\u9047\u5230\u7684\u6700\u5927\u503c\u3002<\/p>\n<pre>int[] myArray = {1, 5, 3, 9, 7};\nint maxValue = myArray[0]; \/\/ \u5047\u5b9a\u7b2c\u4e00\u4e2a\u5143\u7d20\u4e3a\u6700\u5927\u503c\nfor (int i = 1; i &lt; myArray.length; i++) {\n    if (myArray[i] &gt; maxValue) {\n        maxValue = myArray[i];\n    }\n}\nSystem.out.println(\"\u6700\u5927\u503c\uff1a\" + maxValue);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Stream API<\/strong><\/p>\n<p>Java 8 \u4e2d\u5f15\u5165\u7684 Stream API \u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u65b9\u6cd5\u6765\u6c42\u6700\u5927\u503c\uff1a<\/p>\n<pre>int[] myArray = {1, 5, 3, 9, 7};\nint maxValue = Arrays.stream(myArray).max().getAsInt();\nSystem.out.println(\"\u6700\u5927\u503c\uff1a\" + maxValue);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/strong><\/p>\n<p>\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982 Guava\uff0c\u63d0\u4f9b\u4e86\u9884\u5148\u7f16\u5199\u7684\u7528\u4e8e\u6c42\u6700\u5927\u503c\u7684\u51fd\u6570\uff1a<\/p>\n<pre>import com.google.common.base.Functions;\nimport com.google.common.collect.Ordering;\n\nint[] myArray = {1, 5, 3, 9, 7};\nint maxValue = Ordering.natural().max(myArray);\nSystem.out.println(\"\u6700\u5927\u503c\uff1a\" + maxValue);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u6c42\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709 4 \u79cd\u65b9\u6cd5\u6c42 java \u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\uff1a\u4f7f\u7528 arrays.sort()\u4f7f\u7528\u5faa\u73af\u4f7f\u7528 stream api\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93 \u5982\u4f55\u6c42 Java \u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\uff1f \u5728 Java \u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6c42\u51fa\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\uff1a 1. \u4f7f\u7528 Arrays.sort() Arrays.sort() \u65b9\u6cd5\u6309\u7167\u5347\u5e8f\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u5927\u503c\u5c06\u4f4d\u4e8e\u6570\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] myArray = {1, 5, 3, 9, 7}; Arrays.sort(myArray); int maxValue = myArray[myArray.length &#8211; 1]; System.out.println(&#8220;\u6700\u5927\u503c\uff1a&#8221; + maxValue); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u5faa\u73af \u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u8bb0\u5f55\u9047\u5230\u7684\u6700\u5927\u503c\u3002 int[] myArray = {1, 5, 3, 9, 7}; int maxValue = myArray[0]; \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-66938","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66938","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=66938"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66938\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}