{"id":38737,"date":"2024-11-26T17:42:14","date_gmt":"2024-11-26T09:42:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38737\/"},"modified":"2024-11-26T17:42:14","modified_gmt":"2024-11-26T09:42:14","slug":"java%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8d%87%e5%ba%8f%e9%99%8d%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38737\/","title":{"rendered":"java\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u5347\u5e8f\u964d\u5e8f"},"content":{"rendered":"<blockquote><p>\n  java \u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f\u53ef\u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u518d\u8f6c\u6362\u56de\u4e8c\u7ef4\u6570\u7ec4\uff1b\u4f7f\u7528 arrays.parallelsort() \u65b9\u6cd5\u548c\u81ea\u5b9a\u4e49 comparator \u63a5\u53e3\uff0c\u9488\u5bf9\u884c\u6216\u5217\u6bd4\u8f83\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111505453592515.jpg\" class=\"aligncenter\" title=\"java\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u5347\u5e8f\u964d\u5e8f\u63d2\u56fe\" alt=\"java\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u5347\u5e8f\u964d\u5e8f\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong> \u5982\u4f55\u5bf9 Java \u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6216\u964d\u5e8f\u6392\u5e8f\uff1f<\/p>\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u8981\u5bf9 Java \u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u4e00\u3001\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\uff08\u4ec5\u9002\u7528\u4e8e\u4e00\u7ef4\u6570\u7ec4\uff09\uff1a<\/strong><\/p>\n<ol>\n<li>\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\u3002<\/li>\n<li>\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li>\u5c06\u6392\u5e8f\u540e\u7684\u4e00\u7ef4\u6570\u7ec4\u91cd\u65b0\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002<\/li>\n<\/ol>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[][] array = {\n    {1, 4, 7},\n    {2, 5, 8},\n    {3, 6, 9}\n};\n\n\/\/ \u8f6c\u6362\u4e8c\u7ef4\u6570\u7ec4\u4e3a\u4e00\u7ef4\u6570\u7ec4\nint[] flattenedArray = Arrays.stream(array)\n    .flatMapToInt(Arrays::stream)\n    .toArray();\n\n\/\/ \u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\nArrays.sort(flattenedArray);\n\n\/\/ \u8f6c\u6362\u56de\u4e8c\u7ef4\u6570\u7ec4\nint[][] sortedArray = new int[array.length][array[0].length];\nint index = 0;\nfor (int i = 0; i &lt; array.length; i++) {\n    for (int j = 0; j &lt; array[i].length; j++) {\n        sortedArray[i][j] = flattenedArray[index++];\n    }\n}\n\n\/\/ \u5347\u5e8f\u6392\u5e8f\u5b8c\u6210<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e8c\u3001\u4f7f\u7528 Arrays.parallelSort() \u548c Comparator \u63a5\u53e3\uff1a<\/strong><\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684 Comparator \u5b9e\u73b0\uff0c\u7528\u4e8e\u6bd4\u8f83\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u884c\u6216\u5217\u3002<\/li>\n<li>\u4f7f\u7528 Arrays.parallelSort() \u65b9\u6cd5\uff0c\u6307\u5b9a\u81ea\u5b9a\u4e49\u7684 Comparator\u3002<\/li>\n<\/ol>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff08\u5347\u5e8f\u5347\u5e8f\uff09\uff1a<\/strong><\/p>\n<pre>Comparator&lt;int[]&gt; comparator = Comparator.comparingInt(a -&gt; a[0])\n    .thenComparingInt(a -&gt; a[1]);\n\nArrays.parallelSort(array, comparator);\n\n\/\/ \u5347\u5e8f\u5347\u5e8f\u6392\u5e8f\u5b8c\u6210<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff08\u5347\u5e8f\u964d\u5e8f\uff09\uff1a<\/strong><\/p>\n<pre>Comparator&lt;int[]&gt; comparator = Comparator.comparingInt(a -&gt; a[0])\n    .thenComparingInt(a -&gt; -a[1]);\n\nArrays.parallelSort(array, comparator);\n\n\/\/ \u5347\u5e8f\u964d\u5e8f\u6392\u5e8f\u5b8c\u6210<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e8e\u5927\u578b\u4e8c\u7ef4\u6570\u7ec4\uff0c\u4f7f\u7528 Arrays.parallelSort() \u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li>\u81ea\u5b9a\u4e49\u7684 Comparator \u8fd8\u53ef\u4ee5\u7528\u4e8e\u6839\u636e\u4e0d\u540c\u7684\u6bd4\u8f83\u5668\u6761\u4ef6\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u5347\u5e8f\u964d\u5e8f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f\u53ef\u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a\u4f7f\u7528 arrays.sort() \u65b9\u6cd5\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u518d\u8f6c\u6362\u56de\u4e8c\u7ef4\u6570\u7ec4\uff1b\u4f7f\u7528 arrays.parallelsort() \u65b9\u6cd5\u548c\u81ea\u5b9a\u4e49 comparator \u63a5\u53e3\uff0c\u9488\u5bf9\u884c\u6216\u5217\u6bd4\u8f83\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 Java \u4e8c\u7ef4\u6570\u7ec4\u6392\u5e8f \u95ee\u9898\uff1a \u5982\u4f55\u5bf9 Java \u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6216\u964d\u5e8f\u6392\u5e8f\uff1f \u65b9\u6cd5\uff1a \u8981\u5bf9 Java \u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4e00\u3001\u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\uff08\u4ec5\u9002\u7528\u4e8e\u4e00\u7ef4\u6570\u7ec4\uff09\uff1a \u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\u3002 \u4f7f\u7528 Arrays.sort() \u65b9\u6cd5\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 \u5c06\u6392\u5e8f\u540e\u7684\u4e00\u7ef4\u6570\u7ec4\u91cd\u65b0\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a int[][] array = { {1, 4, 7}, {2, 5, 8}, {3, 6, 9} }; \/\/ \u8f6c\u6362\u4e8c\u7ef4\u6570\u7ec4\u4e3a\u4e00\u7ef4\u6570\u7ec4 int[] flattenedArray = Arrays.stream(array) .flatMapToInt(Arrays::stream) .toArray(); \/\/ \u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f Arrays.sort(flattenedArray); \/\/ \u8f6c\u6362\u56de\u4e8c\u7ef4\u6570\u7ec4 [&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-38737","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38737","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=38737"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38737\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}