{"id":45290,"date":"2023-12-11T15:05:24","date_gmt":"2023-12-11T07:05:24","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=45290"},"modified":"2023-12-11T15:05:24","modified_gmt":"2023-12-11T07:05:24","slug":"treemap%e5%92%8ctreeset%e5%9c%a8%e6%8e%92%e5%ba%8f%e6%97%b6%e5%a6%82%e4%bd%95%e6%af%94%e8%be%83%e5%85%83%e7%b4%a0%ef%bc%9fcollections%e5%b7%a5%e5%85%b7%e7%b1%bb%e4%b8%ad%e7%9a%84sort%e6%96%b9-2","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/treemap%e5%92%8ctreeset%e5%9c%a8%e6%8e%92%e5%ba%8f%e6%97%b6%e5%a6%82%e4%bd%95%e6%af%94%e8%be%83%e5%85%83%e7%b4%a0%ef%bc%9fcollections%e5%b7%a5%e5%85%b7%e7%b1%bb%e4%b8%ad%e7%9a%84sort%e6%96%b9-2\/","title":{"rendered":"TreeMap\u548cTreeSet\u5728\u6392\u5e8f\u65f6\u5982\u4f55\u6bd4\u8f83\u5143\u7d20\uff1fCollections\u5de5\u5177\u7c7b\u4e2d\u7684sort()\u65b9\u6cd5\u5982\u4f55\u6bd4\u8f83\u5143\u7d20\uff1f"},"content":{"rendered":"<p>&#8220;`&#8221;                    \u8003\u5bdf\u70b9\uff1aTree<\/p>\n<p>\u00a0<\/p>\n<p>TreeSet\u8981\u6c42\u5b58\u653e\u7684\u5bf9\u8c61\u6240\u5c5e\u7684\u7c7b\u5fc5\u987b\u5b9e\u73b0Comparable\u63a5\u53e3\uff0c\u8be5\u63a5\u53e3\u63d0\u4f9b\u4e86\u6bd4\u8f83\u5143\u7d20\u7684compareTo()\u65b9\u6cd5\uff0c\u5f53\u63d2\u5165\u5143\u7d20\u65f6\u4f1a\u56de\u8c03\u8be5\u65b9\u6cd5\u6bd4\u8f83\u5143\u7d20\u7684\u5927\u5c0f\u3002TreeMap\u8981\u6c42\u5b58\u653e\u7684\u952e\u503c\u5bf9\u6620\u5c04\u7684\u952e\u5fc5\u987b\u5b9e\u73b0Comparable\u63a5\u53e3\u4ece\u800c\u6839\u636e\u952e\u5bf9\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002Collections\u5de5\u5177\u7c7b\u7684sort\u65b9\u6cd5\u6709\u4e24\u79cd\u91cd\u8f7d\u7684\u5f62\u5f0f\uff0c\u7b2c\u4e00\u79cd\u8981\u6c42\u4f20\u5165\u7684\u5f85\u6392\u5e8f\u5bb9\u5668\u4e2d\u5b58\u653e\u7684\u5bf9\u8c61\u6bd4\u8f83\u5b9e\u73b0Comparable\u63a5\u53e3\u4ee5\u5b9e\u73b0\u5143\u7d20\u7684\u6bd4\u8f83\uff1b\u7b2c\u4e8c\u79cd\u4e0d\u5f3a\u5236\u6027\u7684\u8981\u6c42\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\u5fc5\u987b\u53ef\u6bd4\u8f83\uff0c\u4f46\u662f\u8981\u6c42\u4f20\u5165\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u53c2\u6570\u662fComparator\u63a5\u53e3\u7684\u5b50\u7c7b\u578b\uff08\u9700\u8981\u91cd\u5199compare\u65b9\u6cd5\u5b9e\u73b0\u5143\u7d20\u7684\u6bd4\u8f83\uff09\uff0c\u76f8\u5f53\u4e8e\u4e00\u4e2a\u4e34\u65f6\u5b9a\u4e49\u7684\u6392\u5e8f\u89c4\u5219\uff0c\u5176\u5b9e\u5c31\u662f\u901a\u8fc7\u63a5\u53e3\u6ce8\u5165\u6bd4\u8f83\u5143\u7d20\u5927\u5c0f\u7684\u7b97\u6cd5\uff0c\u4e5f\u662f\u5bf9\u56de\u8c03\u6a21\u5f0f\u7684\u5e94\u7528\uff08Java\u4e2d\u5bf9\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u652f\u6301\uff09\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-java&quot;&quot; lang=&quot;&quot;java&quot;&quot;&gt;public class<br \/>\nStudent implements Comparable&lt;Student&gt; {<br \/>\n     private Stringname;        \/\/ \u59d3\u540d<br \/>\n     private int age;            \/\/ \u5e74\u9f84<br \/>\n     public Student(String name, int age) {<br \/>\n         this.name = name;<br \/>\n         this.age = age;<br \/>\n     }<br \/>\n     @Override<br \/>\n     public String toString() {<br \/>\n         return &quot;&quot;Student [name=&quot;&quot; +name + &quot;&quot;, age=&quot;&quot; + age + &quot;&quot;]&quot;&quot;;<br \/>\n     }<br \/>\n     @Override<br \/>\n     public int compareTo(Student o) {<br \/>\n         return this.age &#8211; o.age; \/\/ \u6bd4\u8f83\u5e74\u9f84(\u5e74\u9f84\u7684\u5347\u5e8f)<br \/>\n     }<br \/>\n }<br \/>\n import java.util.Set;<br \/>\n import java.util.TreeSet;<br \/>\n class Test01 {<br \/>\n     public static void main(String[] args) {<br \/>\n         Set&lt;Student&gt; set = new TreeSet&lt;&gt;();     \/\/ Java 7<br \/>\n     }<br \/>\n }<br \/>\n import java.util.Set;<br \/>\n import java.util.TreeSet;<br \/>\n class Test01 {<br \/>\n     public static void main(String[] args) {<br \/>\n         Set&lt;Student&gt; set = new TreeSet&lt;&gt;();     \/\/ Java 7\u7684\u94bb\u77f3\u8bed\u6cd5(\u6784\u9020\u5668\u540e\u9762\u7684\u5c16\u62ec\u53f7\u4e2d\u4e0d\u9700\u8981\u5199\u7c7b\u578b)<br \/>\n         set.add(new Student(&quot;&quot;HaoLUO&quot;&quot;, 33));<br \/>\n         set.add(new Student(&quot;&quot;XJWANG&quot;&quot;, 32));<br \/>\n         set.add(new Student(&quot;&quot;BruceLEE&quot;&quot;, 60));<br \/>\n         set.add(new Student(&quot;&quot;BobYANG&quot;&quot;, 22));<\/p>\n<p>         for(Student stu : set) {<br \/>\n             System.out.println(stu);<br \/>\n         }<br \/>\n \/\/<br \/>\n         set.add(new Student(&quot;&quot;HaoLUO&quot;&quot;, 33));<br \/>\n         set.add(new Student(&quot;&quot;XJWANG&quot;&quot;, 32));<br \/>\n         set.add(new Student(&quot;&quot;BruceLEE&quot;&quot;, 60));<br \/>\n         set.add(new Student(&quot;&quot;BobYANG&quot;&quot;, 22));<br \/>\n         for(Student stu : set) {<br \/>\n             System.out.println(stu);<br \/>\n         }<br \/>\n \/\/      \u8f93\u51fa\u7ed3\u679c:<br \/>\n \/\/      Student [name=Bob YANG, age=22]<br \/>\n \/\/      Student [name=XJ WANG, age=32]<br \/>\n \/\/      Student [name=Hao LUO, age=33]<br \/>\n \/\/      Student [name=Bruce LEE, age=60]<br \/>\n     }<br \/>\n }<br \/>\n \/\/      Student [name=Bob YANG, age=22]<br \/>\n \/\/      Student [name=XJ WANG, age=32]<br \/>\n \/\/      Student [name=Hao LUO, age=33]<br \/>\n \/\/      Student [name=Bruce LEE, age=60]<br \/>\n     }<br \/>\n }<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; \u8003\u5bdf\u70b9\uff1aTree \u00a0 TreeSet\u8981\u6c42\u5b58\u653e\u7684\u5bf9\u8c61\u6240\u5c5e\u7684\u7c7b\u5fc5\u987b\u5b9e\u73b0Compar [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-45290","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/45290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=45290"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/45290\/revisions"}],"predecessor-version":[{"id":45291,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/45290\/revisions\/45291"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=45290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=45290"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=45290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}