{"id":2548,"date":"2022-11-22T02:37:40","date_gmt":"2022-11-21T18:37:40","guid":{"rendered":"http:\/\/tihar-tech.cn\/?p=2548"},"modified":"2023-06-06T17:24:09","modified_gmt":"2023-06-06T09:24:09","slug":"%e8%87%aa%e5%ae%9a%e4%b9%89%e6%8e%92%e5%ba%8f%e8%a7%84%e5%88%99-comparable%e5%92%8ccomparator%e6%8e%a5%e5%8f%a3%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"http:\/\/139.196.114.170\/?p=2548","title":{"rendered":"\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219 &#8211; Comparable\u548cComparator\u63a5\u53e3\u8be6\u89e3"},"content":{"rendered":"<h3>Comparable\u63a5\u53e3\u4e0e\u4f7f\u7528<\/h3>\n<h4>\u63a5\u53e3\u5b9a\u4e49<\/h4>\n<pre lang=\"java\">\npublic interface Comparable <T> {\n    public int compareTo(T o);\n}\n<\/pre>\n<p>&lt;T&gt;\u9650\u5b9a\u53c2\u4e0e\u6bd4\u8f83\u7684\u5bf9\u8c61\u7c7b\u578b\u53ea\u80fd\u4e3aT<br \/>\ncompareTo()\u65b9\u6cd5\u8fd4\u56de\u8c03\u7528\u65b9\u548c\u53c2\u6570\u65b9\u7684\u5927\u5c0f\u5173\u7cfb<\/p>\n<pre lang=\"java\">\n\u8c03\u7528\u5bf9\u8c61 > \u53c2\u6570\u5bf9\u8c61\uff0c\u8fd4\u56de\u503c > 0\n\u8c03\u7528\u5bf9\u8c61 = \u53c2\u6570\u5bf9\u8c61\uff0c\u8fd4\u56de\u503c = 0\n\u8c03\u7528\u5bf9\u8c61 < \u53c2\u6570\u5bf9\u8c61\uff0c\u8fd4\u56de\u503c < 0\n<\/pre>\n<h4>\u6848\u4f8b\u5b9e\u73b0<\/h4>\n<pre><code class=\"language-java\">public class Person implements Comparable&lt;Person&gt; {\n\n    private int age;\n    private String name;\n\n    public Person(int age, String name) {\n        this.age = age;\n        this.name = name;\n    }\n\n    @Override\n    public int compareTo(Person o) {\n        if (this.age != o.age) return Integer.compare(this.age, o.age);\n        return String.CASE_INSENSITIVE_ORDER.compare(this.name, o.name);\n    }\n\n    public static void main(String[] args) {\n        \/\/ test1\n        Person a1 = new Person(24, &quot;Alan&quot;);\n        Person a2 = new Person(24, &quot;Bob&quot;);\n        System.out.println(a1.compareTo(a2));\n        \/\/ test2\n        Person b1 = new Person(24, &quot;Cindy&quot;);\n        Person b2 = new Person(20, &quot;Dory&quot;);\n        System.out.println(b1.compareTo(b2));\n        \/\/ test3\n        Person c1 = new Person(30, &quot;Era&quot;);\n        Person c2 = new Person(30, &quot;Era&quot;);\n        System.out.println(c1.compareTo(c2));\n    }\n}<\/code><\/pre>\n<h4>\u4f7f\u7528\u8981\u70b9<\/h4>\n<p>1\uff09\u591a\u4e2a\u5b57\u6bb5\u6392\u5e8f\u89c4\u5219\u65f6\u6d89\u53ca\u5b57\u6bb5\u4f18\u5148\u7ea7\uff0c\u5728\u7b2c\u4e00\u987a\u4f4d\u503c\u76f8\u540c\u65f6\u6309\u7167\u7b2c\u4e8c\u987a\u4f4d\u503c\u6392\u5e8f\uff0c\u4f9d\u6b64\u7c7b\u63a8<\/p>\n<p>2\uff09<strong>\u57fa\u672c\u7c7b\u578b\u6bd4\u8f83\u65f6\uff0c\u56e0\u6d89\u53ca\u6570\u636e\u8868\u793a\u7ec6\u8282\uff0c\u5efa\u8bae\u4f7f\u7528\u5305\u88c5\u7c7b\u578b\u7684\u9759\u6001\u6bd4\u8f83\u65b9\u6cd5<\/strong>\uff0c\u8be6\u89c1\u4e0b\u4f8b<\/p>\n<ul>\n<li>\n<p>\u6574\u6570\u51cf\u6cd5\u53d1\u751f\u4e0b\u6ea2\uff0c\u5982 <code class=\"katex-inline\">\\scriptsize -2147483648 - 1 = 2147483647<\/code><\/p>\n<pre lang=\"java\">\npublic int compareTo(Object o) {\nreturn this.intVal - o.intVal;\n}\n<\/pre>\n<\/li>\n<li>\n<p>\u6d6e\u70b9\u6570\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u6574\u6570\u65f6\u5411\u5076\u6570\u820d\u5165\uff0c\u4e22\u5931\u8fd0\u7b97\u7ed3\u679c\u7b26\u53f7\u4f4d\uff0c\u5982 <code class=\"katex-inline\">\\scriptsize (int)(0.03003-0.03005) = 0<\/code><\/p>\n<pre lang=\"java\">\npublic int compareTo(Object o) {\nreturn (int) this.doubleVal - o.doubleVal;\n}\n<\/pre>\n<\/li>\n<li>\n<p>\u6d6e\u70b9\u6570\u6bd4\u8f83\u65f6\u4efb\u4e00\u6570\u4e3a\u542b\u6709NAN<\/p>\n<\/li>\n<li>\n<p>\u6d6e\u70b9\u6570\u6bd4\u8f83\u65f6-0.0 &lt; +0.0\u9ed8\u8ba4\u4e3afalse<\/p>\n<\/li>\n<\/ul>\n<h3>\u53c2\u8003\u8d44\u6599<\/h3>\n<p>Bloch J. Effective Java[M]. Third Edition. \u673a\u68b0\u5de5\u4e1a\u51fa\u7248\u793e, 2018.<br \/>\nRandal E.Bryant, David O'Hallaron. Computer Systems: A Programmer's Perspective[M]. 3. \u673a\u68b0\u5de5\u4e1a\u51fa\u7248\u793e, 2016.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Comparable\u63a5\u53e3\u4e0e\u4f7f\u7528 \u63a5\u53e3\u5b9a\u4e49 public interface Comparable { publ &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/139.196.114.170\/?p=2548\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219 &#8211; Comparable\u548cComparator\u63a5\u53e3\u8be6\u89e3\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[25],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/2548"}],"collection":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2548"}],"version-history":[{"count":9,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/2548\/revisions"}],"predecessor-version":[{"id":2853,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/2548\/revisions\/2853"}],"wp:attachment":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2548"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}