{"id":156285,"date":"2020-09-17T09:08:36","date_gmt":"2020-09-17T01:08:36","guid":{"rendered":"http:\/\/4563.org\/?p=156285"},"modified":"2020-09-18T04:10:55","modified_gmt":"2020-09-17T20:10:55","slug":"java-%e6%95%b0%e7%bb%84%ef%bc%8c%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87%e8%87%aa%e5%ae%9a%e4%b9%89%e5%ae%9e%e7%8e%b0%e5%80%92%e5%8f%99%e6%8e%92%e5%ba%8f%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=156285","title":{"rendered":"Java \u6570\u7ec4\uff0c\u5982\u4f55\u901a\u8fc7\u81ea\u5b9a\u4e49\u5b9e\u73b0\u5012\u53d9\u6392\u5e8f\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Java \u6570\u7ec4\uff0c\u5982\u4f55\u901a\u8fc7\u81ea\u5b9a\u4e49\u5b9e\u73b0\u5012\u53d9\u6392\u5e8f\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <span><i><\/i> 7<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5982\u9898\uff0c\u521d\u5b66 java\uff0c\u719f\u6089\u8bed\u6cd5\u8fc7\u7a0b\u4e2d\u9047\u5230\u95ee\u9898\uff0c\u4ee3\u7801\u6bb5\u5982\u4e0b<\/p>\n<pre><code>import java.util.Arrays;  public class Main {     public static void main(String[] args) {         int[] ns = {5,4,3,2,1};         Arrays.sort(ns , (a,b) -&gt; a&gt;=b);         System.out.println(Arrays.toString(ns));     } } <\/code><\/pre>\n<p>\u5e0c\u671b\u5b9e\u73b0\u7684\u6548\u679c\uff1a\u6b63\u5e8f\u6392\u5e8f\u4e0e\u5012\u53d9\u6392\u5e8f\u3002<\/p>\n<p>\u6309\u7167\u5176\u4ed6\u8bed\u8a00\u7684\u903b\u8f91\uff0c\u7ed9\u5b9a\u7684 sort \u51fd\u6570\u4e00\u822c\u662f\u7528\u5feb\u6392\u4e4b\u7c7b\u7684\u7b97\u6cd5\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5982\u679c\u8981\u5b9e\u73b0\u5012\u53d9\u6392\u5e8f\u7684\u8bdd\uff0c\u4e00\u822c\u662f\u5728\u540e\u9762\u8f93\u5165\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5224\u65ad\u51fd\u6570\uff0c\u7528\u4f5c\u5224\u65ad\u5927\u5c0f\u3002<\/p>\n<p>\u7136\u540e\u5728\u8fd9\u6bb5\u4ee3\u7801\u91cc\u8fd9\u4e2a\u6392\u5e8f\u51fd\u6570\u5c31\u5e94\u8be5\u662f\u770b\u8d77\u6765\u5f88\u50cf js \u7684\u7bad\u5934\u51fd\u6570\u7684\u8fd9\u4e2a<code>[(a,b) -&gt; a&gt;=b ]<\/code><\/p>\n<p>\u4ee3\u7801\u65e0\u6cd5\u8fd0\u884c\uff0c\u62a5\u9519<code>[\u8fd0\u7b97\u7b26 '&gt;=' \u4e0d\u80fd\u5e94\u7528\u4e8e 'T', 'T']<\/code>\u3002\u65e0\u6cd5\u7406\u89e3\u53d1\u751f\u4e86\u4ec0\u4e48\uff0c\u4e0d\u7406\u89e3\u4e3a\u4ec0\u4e48\u65e0\u6cd5\u6267\u884c\u3002\u53e6\u5916\u5173\u4e8e\u8fd9\u4e2a\u7c7b\u4f3c\u7bad\u5934\u51fd\u6570\u7684\uff0c\u5e94\u8be5\u662f java \u7684 lambda \u51fd\u6570\u5427\uff0c\u4e0d\u5b9a\u4e49\u8f93\u5165\u7c7b\u578b\u4ec0\u4e48\u7684\u771f\u7684\u6ca1\u6709\u641e\u9519\u5417\uff1f<\/p>\n<p>=========================================================<\/p>\n<p>\u4e0a\u6587\u662f\u6574\u6570\u6392\u5e8f\uff0c\u6362\u6210\u5b57\u7b26\u4e32\u7684\u8bdd\u5c31\u53ef\u4ee5\u8fd0\u884c\u4e86\uff0c\u4e0d\u7406\u89e3\u4e3a\u4ec0\u4e48<\/p>\n<pre><code>import java.util.Arrays;  public class Main {     public static void main(String[] args) {         String[] ns = {\"5\",\"4\",\"3\",\"2\",\"1\"};         Arrays.sort(ns , (a,b) -&gt; a.compareTo(b)); \/\/ \u4e3a\u4ec0\u4e48\u7528\u5927\u4e8e\u5224\u65ad\u4e0d\u884c\uff0c\u7528 string \u7684\u65b9\u6cd5\u5224\u65ad\u5c31\u884c\u4e86         System.out.println(Arrays.toString(ns));     } } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>16<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3269074\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : EminemW <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5e94\u8be5\u662f\u8fd9\u6837 (a,b) -&gt; {b-a}                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269075\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : EminemW <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u770b Arrays.sort() \u65b9\u6cd5\u7684\u53c2\u6570\u5c31\u77e5\u9053\u4e86\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u8fd9\u4e2a\u63a5\u53e3\u8981\u5b9e\u73b0\u6bd4\u8f83\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u6574\u7684\u3002\u4f60\u5199\u7684 lambda \u8fd4\u56de\u7684\u662f\u5e03\u5c14\u578b\uff0c\u5f53\u7136\u4e0d\u53ef\u4ee5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269076\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @EminemW \u8fd8\u662f\u62a5\u9519<br \/>![]( <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/09\/4CmfR27Sl.png\" alt=\"Java \u6570\u7ec4\uff0c\u5982\u4f55\u901a\u8fc7\u81ea\u5b9a\u4e49\u5b9e\u73b0\u5012\u53d9\u6392\u5e8f\uff1f\">)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269077\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Cbdy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             (a,b) -&gt; b-a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269078\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Cbdy \u62a5\u9519\uff0c\u62a5\u9519\u4fe1\u606f\u76f8\u540c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269079\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : KaraSalmon <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6570\u7ec4\u7528 Integer                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269080\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : aguesuka <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e2a\u5f88\u660e\u663e\u662f\u56e0\u4e3a int \u662f\u57fa\u672c\u7c7b\u578b\u4e0d\u662f\u5bf9\u8c61\uff0c\u6bd4\u8f83\u51fd\u6570\u7684\u53c2\u6570\u662f\u6cdb\u578b\u5bf9\u8c61\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269081\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lxychn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. Arrays.sort() \u4e0d\u80fd\u5e94\u7528\u4e8e\u539f\u59cb\u7c7b\u578b\u6392\u5e8f\uff0cint \u662f\u539f\u59cb\u7c7b\u578b\uff0cString \u4e0d\u662f<br \/>2. Arrays.sort()\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u5165\u662f\u4e2a Comparator\uff0c\u7528\u7684\u662f\u91cc\u9762\u624d compare \u65b9\u6cd5\uff0ccompare \u65b9\u6cd5\u8fd4\u56de\u7684\u662f int\uff0c\u4e00\u822c\u7528\u8d1f\u503c\uff0c0\uff0c\u6b63\u503c\u4ee3\u8868\u5c0f\u4e8e\uff0c\u7b49\u4e8e\uff0c\u5927\u4e8e\u3002compareTo()\u8fd4\u56de\u7684\u4e5f\u662f int                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269082\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : black11black <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @aguesuka <br \/>@lxychn <br \/>\u611f\u8c22\u56de\u590d\uff0cArrays.sort \u5982\u679c\u4e0d\u52a0 comparator \u7684\u8bdd\u662f\u53ef\u4ee5\u5bf9 int[]\u8fdb\u884c\u6392\u5e8f\u7684\uff0c\u6240\u4ee5\u5e94\u8be5\u4e0d\u662f\u65e0\u6cd5\u6392\u5e8f\uff0c\u6240\u4ee5\u539f\u56e0\u5e94\u8be5\u662f comparator \u65e0\u6cd5\u5e94\u7528\u4e8e\u5355\u7eaf int \u6784\u6210\u7684\u6570\u7ec4\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269083\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ky11223344 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7f16\u8bd1\u65f6\u9759\u6001\u68c0\u67e5\u4e0d\u901a\u8fc7\uff0c\u8fd9\u91cc\u7684 sort \u662f\u4e2a\u6cdb\u578b\u65b9\u6cd5\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u901a\u8fc7\u8f93\u5165\u53c2\u6570\u63a8\u51fa\u7c7b\u578b\u53c2\u6570\u7684\u503c\uff0c\u5982\u679c\u5c1d\u8bd5\u6307\u660e\u7c7b\u578b\u53c2\u6570\u518d call \u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u5f97\u5230\u66f4\u660e\u786e\u7684\u9519\u8bef\u63d0\u793a\uff0c\u50cf Arrays.&lt;int&gt;sort \u4f1a\u63d0\u793a primitive \u4e0d\u80fd\u4f5c\u4e3a\u7c7b\u578b\u53c2\u6570\uff0c\u6216 Arrays.&lt;Integer&gt;sort \u4f1a\u63d0\u793a\u7b2c\u4e00\u4e2a\u53c2\u6570\u7c7b\u578b int[]\u4e0d\u6b63\u786e\u3002\u4e0d\u6307\u660e\u7c7b\u578b\u53c2\u6570\u7684\u8bdd\u5219\u7c7b\u578b\u53c2\u6570 T \u65e0\u6cd5\u88ab\u63a8\u5bfc\uff0c\u5728 lambda \u91cc\u8c03\u7528 a &#8211; b \u5219\u7f16\u8bd1\u5668\u8ba4\u4e3a\u5bf9\u4e24\u4e2a\u672a\u77e5\u7684 T \u7c7b\u578b\u503c\u505a\u53ea\u6709 primitive \u624d\u6709\u6548\u7684\u52a0\u51cf\u4e58\u9664\u7b49\u64cd\u4f5c\u662f\u65e0\u6548\u7684\uff0c\u6240\u4ee5\u5c31\u6709\u90a3\u4e2a\u9519\u8bef\u63d0\u793a\u3002\u8fd9\u91cc\u8fd8\u6709\u4e2a\u95ee\u9898\u5c31\u662f\u8f93\u5165\u53c2\u6570\u7684\u7c7b\u578b\u4e0d\u5339\u914d\u4efb\u4f55\u4e00\u4e2a sort \u65b9\u6cd5\u51fd\u6570\u7b7e\u540d\u65f6\uff0c\u7f16\u8bd1\u5668\u662f\u600e\u4e48\u77e5\u9053\u4f60\u8981\u8c03\u7528\u7684\u5c31\u662f\u8fd9\u4e2a\u6cdb\u578b\u65b9\u6cd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269084\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : vicsun2020 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Comparator \u53ea\u63a5\u53d7\u5305\u88c5\u7c7b\u7684\u6bd4\u8f83\uff0c\u53ea\u7528\u57fa\u7840\u7c7b\u578b\u6ca1\u6cd5\u81ea\u5b9a\u4e49\u6392\u5e8f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269085\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lxychn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @black11black <br \/>\u53ea\u8003\u8651\u4e86\u8fd9\u79cd\u7279\u5b9a\u60c5\u51b5\u3002\u3002\u662f\u7684\uff0cArrays.sort()\u4e0d\u52a0 Comparator \u53ef\u4ee5\u5bf9 int[]\u9012\u589e\u6392\u5e8f\uff0c\u65e0\u6cd5\u81ea\u5b9a\u4e49\u6392\u5e8f\u3002compare \u548c compareTo \u4f20\u5165\u7684\u662f\u7c7b\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269086\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : daozhihun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. \u5982\u679c\u4f60\u4e0d\u4f20 comparator\uff0c\u5c31\u662f\u6b63\u5e8f\u6392\u5217\uff0c\u5982\u679c\u662f\u57fa\u672c\u7c7b\u578b\u5c31\u662f\u57fa\u672c\u7c7b\u578b\u7684\u6bd4\u8f83\u5668\uff0c\u5982\u679c\u662f\u5f15\u7528\u7c7b\u578b\u4f1a\u81ea\u52a8\u8c03\u7528 compareTo \u65b9\u6cd5\u53bb\u6bd4\u8f83<br \/>2. \u4f20\u5165 comparator \u624d\u80fd\u5b9e\u73b0\u4f60\u81ea\u5df1\u60f3\u8981\u7684\u6392\u5e8f\u65b9\u5f0f\uff0c\u6bd4\u5982\u9006\u5e8f\u6216\u8005\u5176\u4ed6\u7684\u3002<br \/>\u4f46\u662f comparator \u8fd9\u4e2a\u91cd\u8f7d\u65b9\u6cd5\u7684\u5b9a\u4e49\u662f T[]\uff0c\u4f60\u8fd9\u4e2a int[]\u662f\u57fa\u672c\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u65e0\u6cd5\u8f6c\u6362\u6210 Object[]<br \/>\u6240\u4ee5\u8981\u4e48\u4f60\u81ea\u5df1\u628a int[]\u5305\u88c5\u6210 Integer[]\uff08\u7528 stream \uff09\uff0c\u8981\u4e48\u4f60\u81ea\u5df1\u628a\u6570\u7ec4\u53cd\u5e8f\u3002<br \/>\u6ca1\u9519\uff0c\u8fd9\u5c31\u662f java \u8fd9\u4e2a\u6cdb\u578b\u86cb\u75bc\u7684\u5730\u65b9                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269087\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Mysqto <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u62a5\u9519\u7684\u539f\u56e0\u662f Arrays.sort()\u4e0d\u652f\u6301 primitive type, \u4f60\u628a int \u6362\u6210 Integer, \u987a\u5e8f\u5012\u53d9\u90fd\u53ef\u4ee5\u7528 lambda<br \/>&#8220;`java<br \/> Integer[] ns = {5,4,3,2,1};<br \/> Arrays.sort(ns , (a,b) -&gt; b-a);<br \/>&#8220;`<\/p>\n<p>&#8220;`java<br \/> Integer[] ns = {5,4,3,2,1};<br \/> Arrays.sort(ns , (a,b) -&gt; a-b);<br \/>&#8220;`<br \/>\u5f53\u7136\u6700\u7b80\u5355\u7684\u4f60\u60f3\u5012\u53d9\u53ef\u4ee5\u7528 `Arrays.sort(ns , Comparator.reverseOrder());`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269088\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 340244120w <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Arrays.stream(ns).boxed().sorted(Comparator.reverseOrder());                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3269089\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xhq <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f7f\u7528 Guava<br \/>Ints.sortDescending(new int[]{1, 2, 3, 4});                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Java \u6570\u7ec4\uff0c\u5982\u4f55\u901a\u8fc7\u81ea\u5b9a\u4e49\u5b9e\u73b0&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/156285"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=156285"}],"version-history":[{"count":1,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/156285\/revisions"}],"predecessor-version":[{"id":156287,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/156285\/revisions\/156287"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=156285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=156285"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=156285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}