{"id":55233,"date":"2025-08-13T19:44:54","date_gmt":"2025-08-13T11:44:54","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/55233.html"},"modified":"2025-08-13T19:44:54","modified_gmt":"2025-08-13T11:44:54","slug":"%e8%87%aa%e7%84%b6%e6%8e%92%e5%ba%8fvs%e6%af%94%e8%be%83%e5%99%a8%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/55233.html","title":{"rendered":"\u81ea\u7136\u6392\u5e8fVS\u6bd4\u8f83\u5668\u6392\u5e8f"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E5%89%8D%E8%A8%80%EF%BC%9A-toc\" style=\"margin-left:40px\">\u524d\u8a00&#xff1a;<\/p>\n<p id=\"%E4%B8%80%E3%80%81%E8%87%AA%E7%84%B6%E6%8E%92%E5%BA%8F-toc\" style=\"margin-left:40px\">\u4e00\u3001\u81ea\u7136\u6392\u5e8f<\/p>\n<p id=\"1.1%20Integer%E7%B1%BB%E5%9E%8B-toc\" style=\"margin-left:80px\">1.1 Integer\u7c7b\u578b<\/p>\n<p id=\"1.2%20String%E7%B1%BB%E5%9E%8B-toc\" style=\"margin-left:80px\">1.2 String\u7c7b\u578b<\/p>\n<p id=\"1.3%20%E8%87%AA%E5%AE%9A%E4%B9%89%E7%B1%BBStudent-toc\" style=\"margin-left:80px\">1.3 \u81ea\u5b9a\u4e49\u7c7bStudent<\/p>\n<p id=\"compareTo%EF%BC%88%EF%BC%89%E6%96%B9%E6%B3%95%E8%AF%B4%E6%98%8E-toc\" style=\"margin-left:80px\">compareTo&#xff08;&#xff09;\u65b9\u6cd5\u8bf4\u660e<\/p>\n<p id=\"1.4%20%E7%BB%83%E4%B9%A0%E4%B8%80-toc\" style=\"margin-left:80px\">1.4 \u7ec3\u4e60\u4e00<\/p>\n<p id=\"%E4%BA%8C%E3%80%81%E6%AF%94%E8%BE%83%E5%99%A8%E6%8E%92%E5%BA%8F-toc\" style=\"margin-left:40px\">\u4e8c\u3001\u6bd4\u8f83\u5668\u6392\u5e8f<\/p>\n<p id=\"2.1%20Test%E4%B8%AD%E7%9A%84%E6%AF%94%E8%BE%83%E5%99%A8-toc\" style=\"margin-left:80px\">2.1 Test\u4e2d\u7684\u6bd4\u8f83\u5668<\/p>\n<p id=\"2.2%20%E7%BB%83%E4%B9%A0%E4%BA%8C-toc\" style=\"margin-left:80px\">2.2 \u7ec3\u4e60\u4e8c<\/p>\n<p id=\"%E4%B8%89%E3%80%81%E8%87%AA%E7%84%B6%E6%8E%92%E5%BA%8FVS%E6%AF%94%E8%BE%83%E5%99%A8%E6%8E%92%E5%BA%8F-toc\" style=\"margin-left:40px\">\u4e09\u3001\u81ea\u7136\u6392\u5e8fVS\u6bd4\u8f83\u5668\u6392\u5e8f<\/p>\n<p id=\"3.1%20%E7%BB%83%E4%B9%A0%E4%B8%89-toc\" style=\"margin-left:80px\">3.1 \u7ec3\u4e60\u4e09<\/p>\n<p id=\"%E5%9B%9B%E3%80%81%E4%B9%A0%E9%A2%98%E7%AD%94%E6%A1%88-toc\" style=\"margin-left:40px\">\u56db\u3001\u4e60\u9898\u7b54\u6848<\/p>\n<p id=\"4.1%20%E7%BB%83%E4%B9%A0%E4%B8%80-toc\" style=\"margin-left:80px\">4.1 \u7ec3\u4e60\u4e00<\/p>\n<p id=\"4.2%20%E7%BB%83%E4%B9%A0%E4%BA%8C-toc\" style=\"margin-left:80px\">4.2 \u7ec3\u4e60\u4e8c<\/p>\n<p id=\"4.3%20%E7%BB%83%E4%B9%A0%E4%B8%89-toc\" style=\"margin-left:80px\">4.3 \u7ec3\u4e60\u4e09<\/p>\n<p id=\"%E4%BA%94%E3%80%81%E6%89%A9%E5%B1%95-toc\" style=\"margin-left:40px\">\u4e94\u3001\u6269\u5c55<\/p>\n<hr id=\"hr-toc\" \/>\n<\/p>\n<h3 id=\"%E5%89%8D%E8%A8%80%EF%BC%9A\">\u524d\u8a00&#xff1a;<\/h3>\n<p><span style=\"color:#e6b223\">\u672c\u7bc7\u6587\u7ae0\u91cd\u70b9\u8ba8\u8bbaTreeSet\u4e2d\u7684\u4e24\u79cd\u6392\u5e8f\u65b9\u5f0f&#xff1a;\u81ea\u7136\u6392\u5e8fVS\u6bd4\u8f83\u5668\u6392\u5e8f\u3002<\/span><\/p>\n<p>\u611f\u8c22\u5927\u5bb6\u7684\u89c2\u770b\u3002<\/p>\n<\/p>\n<p>\u9996\u5148&#xff0c;\u8ba9\u6211\u4eec\u56de\u987e\u4e00\u4e0bTreeSet\u3002<\/p>\n<p>TreeSet\u7684\u5e95\u5c42\u662f\u4e8c\u53c9\u6811&#xff0c;\u6240\u5305\u542b\u7684\u6570\u636e\u5177\u6709\u552f\u4e00\u6027&#xff0c;\u672c\u8eab\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u9ad8\u6548\u7684\u96c6\u5408\u3002<\/p>\n<p>\u5728\u9762\u5bf9\u4f20\u5165\u7684\u6570\u636e\u65f6&#xff0c;TreeSet\u4f1a\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f&#8212;&#8211;\u6709\u5e8f\u3002<\/p>\n<p>\u9996\u5148&#xff0c;\u6211\u4eec\u6765\u4e86\u89e3\u4e00\u4e0b\u5728\u50a8\u5b58\u6211\u4eec\u5e38\u7528\u7684\u6570\u636e\u7c7b\u578b&#xff1a;Integer&#xff0c;String\u65f6&#xff0c;\u5b83\u4f1a\u6709\u4ec0\u4e48\u8868\u73b0\u3002<\/p>\n<p>\u6ce8\u610f&#xff1a;\u96c6\u5408\u53ea\u80fd\u5b58\u50a8\u5f15\u7528\u6570\u636e\u7c7b\u578b&#xff0c;\u9762\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4f1a\u81ea\u52a8\u88c5\u7bb1\u3002<\/p>\n<\/p>\n<h3 id=\"%E4%B8%80%E3%80%81%E8%87%AA%E7%84%B6%E6%8E%92%E5%BA%8F\">\u4e00\u3001\u81ea\u7136\u6392\u5e8f<\/h3>\n<\/p>\n<h4 id=\"1.1%20Integer%E7%B1%BB%E5%9E%8B\">1.1 Integer\u7c7b\u578b<\/h4>\n<p>\u4ee3\u7801\u6f14\u793a&#xff1a;<\/p>\n<p>public class TestTree {<br \/>\n    public static void main(String[] args) {<br \/>\n        Set&lt;Integer&gt; tree&#061;new TreeSet&lt;&gt;();<br \/>\n        tree.add(1);<br \/>\n        tree.add(3);<br \/>\n        tree.add(2);<br \/>\n        tree.add(7);<br \/>\n        tree.add(5);<br \/>\n        tree.add(6);<br \/>\n        tree.add(11);<br \/>\n        tree.add(33);<br \/>\n        System.out.println(tree);\/\/[1, 2, 3, 5, 6, 7, 11, 33]\u4f1a\u81ea\u52a8\u6392\u5e8f&#8212;\u5927\u5c0f<br \/>\n} <\/p>\n<p>\u5927\u5bb6\u53ef\u4ee5\u770b\u5230&#xff0c;\u6211\u4eec\u7684TreeSet\u662f\u6709\u5e8f\u7684&#xff0c;\u9762\u5bf9Integer\u7c7b\u578b\u65f6&#xff0c;\u5b83\u4f1a\u6309\u7167\u5927\u5c0f\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<h4 id=\"1.2%20String%E7%B1%BB%E5%9E%8B\">1.2 String\u7c7b\u578b<\/h4>\n<p>\u4ee3\u7801\u6f14\u793a&#xff1a;<\/p>\n<p>      Set&lt;String&gt; tree2&#061;new TreeSet&lt;&gt;();<br \/>\n      tree2.add(&#034;1&#034;);<br \/>\n      tree2.add(&#034;3&#034;);<br \/>\n      tree2.add(&#034;2&#034;);<br \/>\n      tree2.add(&#034;7&#034;);<br \/>\n      tree2.add(&#034;5&#034;);<br \/>\n      tree2.add(&#034;6&#034;);<br \/>\n      tree2.add(&#034;11&#034;);<br \/>\n      tree2.add(&#034;33&#034;);<br \/>\n      System.out.println(tree2);\/\/[1, 11, 2, 3, 33, 5, 6, 7]\u6309\u7167\u7b2c\u4e00\u4e2a\u5b57\u7b26\u6392\u5e8f <\/p>\n<p>\u6211\u4eec\u8fd9\u91cc\u4e3a\u4e86\u65b9\u4fbf\u5927\u5bb6\u4f5c\u6bd4\u8f83&#xff0c;\u9009\u62e9\u4e86\u4e0eInteger\u7c7b\u578b\u91c7\u7684\u6570\u636e\u57fa\u672c\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<p>\u5927\u5bb6\u53ef\u4ee5\u770b\u5230&#xff0c;\u8fd9\u91cc\u6211\u4eec\u7684\u5b57\u7b26\u4e32\u662f\u5b89\u88c5\u7b2c\u4e00\u4e2a\u5b57\u7b26\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p>\u90a3\u4e48\u5c31\u6709\u4eba\u4f1a\u95ee&#xff0c;<span style=\"color:#be191c\">\u4e3a\u4ec0\u4e4811\u6392\u57281\u540e\u9762&#xff0c;\u800c\u4e0d\u6392\u57281\u524d\u9762&#xff1f;<\/span><\/p>\n<p><span style=\"color:null\">\u5f88\u7b80\u5355&#xff0c;\u56e0\u4e3a\u6211\u4eec\u6392\u5b8c\u7b2c\u4e00\u4e2a&#xff0c;\u8fd8\u8981\u6392\u7b2c\u4e8c\u4e2a\u3002<\/span><\/p>\n<p>\u6240\u4ee5&#xff1a;<\/p>\n<p><span style=\"color:#e6b223\">\u81ea\u7136\u6392\u5e8f\u4e2d\u7684String\u7c7b\u578b\u6392\u5e8f\u89c4\u5219\u5982\u4e0b&#xff1a;<\/span><\/p>\n<p><span style=\"color:#e6b223\">&#8211;\u6309\u5b57\u7b26\u987a\u5e8f\u9010\u4f4d\u6bd4\u8f83&#xff1b;<\/span><\/p>\n<p><span style=\"color:#e6b223\">&#8211;\u533a\u5206\u5927\u5c0f\u5199&#xff1a;<\/span><\/p>\n<p><span style=\"color:#e6b223\">&#8211;\u5728\u6392\u5e8f\u7b49\u7ea7\u4e0a&#8212;-\u6570\u5b57&gt;\u5927\u5199\u5b57\u6bcd&gt;\u5c0f\u5199\u5b57\u6bcd<\/span><\/p>\n<\/p>\n<p><span style=\"color:#e6b223\">&#xff08;0,1,2&#8230;&#xff09;&gt;&#xff08;A,B,C&#8230;&#8230;&#xff09;&gt;&#xff08;a,b,c&#8230;&#xff09;<\/span><\/p>\n<p>\u6ce8\u610f&#xff1a;\u6b63\u5982\u540c\u6211\u4eec1\u6392\u57282\u524d\u9762\u4e00\u6837&#xff0c;\u6240\u4ee5A\u7684\u503c&#xff08;65&#xff09;\u662f\u6bd4a\u7684\u503c&#xff08;97&#xff09;\u5c0f\u7684\u3002<\/p>\n<p>0\u7684\u503c\u4e3a&#xff08;48&#xff09;\u3002<\/p>\n<\/p>\n<p>&#039;0&#039;&#xff08;48&#xff09;&lt; &#039;1&#039;&#xff08;49&#xff09;&lt; &#8230; &lt; &#039;Z&#039;&#xff08;90&#xff09;&lt; &#039;[&#039;&#xff08;91&#xff09;&lt; &#8230; &lt; &#039;a&#039;&#xff08;97&#xff09;\u3002<\/p>\n<\/p>\n<h4 id=\"1.3%20%E8%87%AA%E5%AE%9A%E4%B9%89%E7%B1%BBStudent\">1.3 \u81ea\u5b9a\u4e49\u7c7bStudent<\/h4>\n<p>\u81ea\u5b9a\u4e49\u7c7b&#xff08;Student&#xff09;&#8212;&gt;\u629b\u51fa\u5f02\u5e38&#8212;&gt;\u65e0\u6cd5\u786e\u5b9a\u6392\u5e8f\u89c4\u5219&#8211;&gt;\u91cd\u5199compareTo\u65b9\u6cd5<\/p>\n<\/p>\n<p>\u8981\u641e\u6e05\u695a\u6211\u4eec\u5982\u4f55\u5c06\u81ea\u7136\u6392\u5e8f\u7528\u5728\u81ea\u5b9a\u4e49\u7c7b\u4e0a&#xff0c;\u6211\u4eec\u5c31\u9700\u8981\u77e5\u9053TreeSet\u81ea\u7136\u6392\u5e8f\u7684\u539f\u7406\u3002<\/p>\n<p><span style=\"color:#e6b223\">TreeSet \u7684\u81ea\u7136\u6392\u5e8f&#xff08;Natural Ordering&#xff09;\u57fa\u4e8e\u5176\u5b58\u50a8\u5143\u7d20\u5b9e\u73b0\u7684Comparable\u63a5\u53e3<\/span><\/p>\n<p>\u901a\u8fc7\u91cd\u5199Comparable\u63a5\u53e3\u4e2d\u7684cmpareTo&#xff08;&#xff09;\u65b9\u6cd5&#xff0c;\u6765\u5b9e\u73b0\u6211\u4eec\u4e0d\u540c\u7c7b\u578b\u4e0b\u7684\u81ea\u7136\u6392\u5e8f\u3002<\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u7684\u81ea\u5b9a\u4e49\u7c7b\u8981\u60f3\u8fdb\u884c\u81ea\u7136\u6392\u5e8f&#xff0c;\u4e5f\u9700\u8981\u63a5\u4e0aComparable\u63a5\u53e3&#xff0c;\u91cd\u5199compareTo&#xff08;&#xff09;\u3002<\/p>\n<\/p>\n<p>\u4ee3\u7801\u6f14\u793a&#xff1a;<\/p>\n<p>public class Student implements Comparable&lt;Student&gt;{<br \/>\n    private String name;<br \/>\n    private int age;<br \/>\n    private String sex;<\/p>\n<p>\/\/&#8230;..\u6784\u9020\u5668&#xff0c;get\u3001set\u7b49&#8230;&#8230;<\/p>\n<p>    &#064;Override<br \/>\n    public String toString() {<br \/>\n        return &#034;\\\\nStudent [name&#061;&#034; &#043; name &#043; &#034;, age&#061;&#034; &#043; age &#043; &#034;, sex&#061;&#034; &#043; sex &#043; &#034;]&#034;;<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public int compareTo(Student o) {<br \/>\n        \/\/\u91cd\u5199\u7684\u81ea\u7136\u6392\u5e8f\u65b9\u6cd5<br \/>\n        \/\/name<br \/>\n        int num;<br \/>\n        num&#061;this.name.compareTo(o.name);\/\/\u8c03\u7528String\u7c7b\u7684compareTo<br \/>\n        if(num&#061;&#061;0){<br \/>\n            \/\/name\u76f8\u540c<br \/>\n            \/\/ages\u5347\u5e8f\u6392<br \/>\n            num&#061;o.age-this.age;<br \/>\n            if(num&#061;&#061;0){<br \/>\n                \/\/age\u4e5f\u76f8\u540c<br \/>\n                \/\/sex<br \/>\n                num&#061;this.sex.compareTo(o.sex);<br \/>\n            }<br \/>\n        }<br \/>\n        return num;<\/p>\n<p>      \/\/  return this.age &#8211; o.age;  \/\/ \u6309\u5e74\u9f84\u5347\u5e8f<br \/>\n    }<br \/>\n}<\/p>\n<\/p>\n<h4 id=\"compareTo%EF%BC%88%EF%BC%89%E6%96%B9%E6%B3%95%E8%AF%B4%E6%98%8E\">compareTo&#xff08;&#xff09;\u65b9\u6cd5\u8bf4\u660e<\/h4>\n<p>\u8fd4\u56de\u503c\u7c7b\u578b&#xff1a;int<\/p>\n<p><span style=\"color:#e6b223\">\u8fd4\u56de-1&#xff1a; this.value&lt;o.value\u00a0 \u00a0 \u00a0\u5f53\u524d\u5bf9\u8c61\u5c0f\u4e8e\u76ee\u6807\u5bf9\u8c61<\/span><\/p>\n<p><span style=\"color:#e6b223\">\u8fd4\u56de0&#xff1a;\u00a0\u00a0this.value&#061;o.value\u00a0 \u00a0 \u00a0\u5f53\u524d\u5bf9\u8c61\u7b49\u4e8e\u76ee\u6807\u5bf9\u8c61<\/span><\/p>\n<p><span style=\"color:#e6b223\">\u8fd4\u56de1&#xff1a;\u00a0\u00a0this.value&gt;o.value\u00a0 \u00a0 \u00a0\u5f53\u524d\u5bf9\u8c61\u5927\u4e8e\u76ee\u6807\u5bf9\u8c61<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u6ce8\u610f&#xff1a;\u5c0f\u7684\u5728\u524d\u9762<\/span><\/p>\n<\/p>\n<p>\u5728\u5f80TreeSet\u4e2d\u63d2\u5165\u5143\u7d20\u65f6&#xff0c;\u9700\u8981\u6392\u5e8f&#043;\u53bb\u91cd\u3002<\/p>\n<p>\u8fd9\u70b9\u4fbf\u901a\u8fc7\u4e0a\u8ff0\u5bf9\u6bd4\u5b9e\u73b0&#xff1a;<\/p>\n<p>(-1)&#043;(&#043;1)&#8212;-\u5b9e\u73b0\u6392\u5e8f<\/p>\n<p>0&#8212;-\u5b9e\u73b0\u53bb\u91cd&#8212;&#8212;\u4e3a0\u53ea\u4fdd\u7559\u7b2c\u4e00\u4e2a\u63d2\u5165\u7684\u5143\u7d20&#xff0c;\u5ffd\u7565\u540e\u7eed\u76f8\u540c\u5143\u7d20<\/p>\n<\/p>\n<p>\u5bf9\u81ea\u5b9a\u4e49\u7c7b\u4f7f\u7528\u81ea\u7136\u6392\u5e8f\u9700\u8981\u6ce8\u610f&#xff1a;<\/p>\n<p>(1)\u63a5\u53e3Comparable&#xff1b;<\/p>\n<p>&#8211;\u81ea\u5b9a\u4e49\u7c7b implements Comparable&lt;\u81ea\u5b9a\u4e49\u7c7b&gt;<\/p>\n<p>(2)\u91cd\u5199compareTo()&#xff1b;<\/p>\n<p>&#8211;public int compareTo(\u81ea\u5b9a\u4e49\u7c7b\u00a0o){}<\/p>\n<\/p>\n<h4 id=\"1.4%20%E7%BB%83%E4%B9%A0%E4%B8%80\" style=\"background-color:transparent\">1.4 \u7ec3\u4e60\u4e00<\/h4>\n<p>\u7528\u81ea\u7136\u6392\u5e8f\u5b9e\u73b0<\/p>\n<p>\u00a0\u57fa\u7840\u7c7b \u00a0 \u00a0 Student \u00a0 \u00a0 \u00a0 \u5c5e\u6027&#xff1a;name age score \u00a0 \u8981\u6c42 \u00a0 \u00a0 \u5c06\u5b66\u2f63\u6309\u7167\u59d3\u540d\u964d\u5e8f\u3001age\u5347\u5e8f&#xff0c;\u6210\u7ee9\u964d\u5e8f\u4fdd\u5b58\u5230\u96c6\u5408\u4e2d \u00a0 \u00a0 \u5e76\u4e14\u540d\u5b57\u53ebtom\u7684\u5b66\u2f63\u4e0d\u7ba1\u8003\u591a\u5c11\u5206\u90fd\u4f4d\u4e8e\u73ed\u7ea7\u7684\u7b2c\u2f00\u4f4d<\/p>\n<p>\u8fd9\u90e8\u5206\u7684\u5185\u5bb9\u867d\u7136\u7b80\u5355&#xff0c;\u4f46\u5927\u5bb6\u4e00\u5b9a\u8981\u52a8\u624b\u7ec3\u4e60\u3002<\/p>\n<\/p>\n<h3 id=\"%E4%BA%8C%E3%80%81%E6%AF%94%E8%BE%83%E5%99%A8%E6%8E%92%E5%BA%8F\">\u4e8c\u3001\u6bd4\u8f83\u5668\u6392\u5e8f<\/h3>\n<p>\u65e2\u7136&#xff0c;\u81ea\u7136\u6392\u5e8f\u90fd\u5982\u6b64\u7684\u5168\u80fd\u4e86&#xff0c;\u5e38\u89c1\u7684\u7c7b\u578b\u548c\u81ea\u5b9a\u4e49\u7c7b\u578b\u5b83\u90fd\u80fd\u89e3\u51b3&#xff0c;\u90a3\u8fd8\u8981\u6211\u4eec\u7684\u6bd4\u8f83\u5668\u6392\u5e8f\u505a\u4ec0\u4e48\u5462&#xff1f;<\/p>\n<p>\u5f53\u7136&#xff0c;\u65e2\u7136\u6211\u4eec\u6562\u628a\u6bd4\u8f83\u5668\u6392\u5e8f\u62ff\u51fa\u6765\u548c\u81ea\u7136\u6392\u5e8f\u78b0\u4e00\u78b0&#xff0c;\u90a3\u4e48\u6bd4\u8f83\u5668\u6392\u5e8f\u80af\u5b9a\u662f\u4e0d\u900a\u8272\u4e8e\u81ea\u7136\u6392\u5e8f\u7684\u3002<\/p>\n<p>\u6bd4\u5982\u601d\u8003\u4ee5\u4e0b\u4e24\u4e2a\u95ee\u9898&#xff1a;<\/p>\n<p><span style=\"color:#be191c\">&#8211;\u5982\u679cStudent\u7c7b\u662f\u7b2c\u4e09\u65b9\u63d0\u4f9b\u7684\u7c7b&#xff0c;\u6211\u4eec\u4e0d\u80fd\u4fee\u6539\u6e90\u4ee3\u7801\u5462&#xff1f;<\/span><\/p>\n<p><span style=\"color:#be191c\">&#8211;\u5982\u679c\u6211\u4eec\u9700\u8981\u5b9e\u73b0\u4e0d\u540c\u7684\u6392\u5e8f\u9700\u6c42&#xff0c;\u6bd4\u5982\u6211\u8fd9\u4e00\u90e8\u5206\u529f\u80fd\u9700\u8981\u5b9e\u73b0\u5347\u5e8f&#xff0c;\u53e6\u4e00\u90e8\u5206\u5b9e\u73b0\u964d\u5e8f\u5462&#xff1f;<\/span><\/p>\n<p>\u5982\u6b64&#xff0c;\u662f\u5426\u6211\u4eec\u5c31\u4e0d\u80fd\u8fd0\u7528\u6211\u4eec\u81ea\u7136\u6392\u5e8f\u7684\u77e5\u8bc6\u4e86\u5462&#xff1f;<\/p>\n<p>\u6bd5\u7adf\u81ea\u7136\u6392\u5e8f\u662f\u5efa\u7acb\u5728\u91cd\u5199\u7684\u57fa\u7840\u4e0a\u7684&#xff0c;\u53ef\u662f\u73b0\u5728\u6211\u4eec\u4fee\u6539Student\u6e90\u4ee3\u7801\u7684\u6743\u5229\u88ab\u5265\u593a\u4e86\u3002<\/p>\n<p>\u8fd9\u4e2a\u65f6\u5019&#xff0c;\u4fbf\u5f15\u51fa\u4e86\u6211\u4eec\u73b0\u5728\u6240\u5b66\u7684\u6392\u5e8f&#8212;&gt;\u6bd4\u8f83\u5668\u6392\u5e8f<\/p>\n<\/p>\n<h4 id=\"2.1%20Test%E4%B8%AD%E7%9A%84%E6%AF%94%E8%BE%83%E5%99%A8\">2.1 Test\u4e2d\u7684\u6bd4\u8f83\u5668<\/h4>\n<p>&#8212;&#8212;\u6bd4\u8f83\u5668\u4e00\u822c\u5199\u5728main\u65b9\u6cd5\u4e2d&#xff0c;\u6bcf\u6b21\u4f7f\u7528\u90fd\u9700\u8981\u5b9a\u4e49\u3002<\/p>\n<p><span style=\"color:#e6b223\">\u6b65\u9aa4&#xff1a;<\/span><\/p>\n<p><span style=\"color:#e6b223\">1.\u521b\u5efa\u4e00\u4e2a\u5b9e\u73b0Comparator\u63a5\u53e3\u7684\u7c7b&#xff1b;<\/span><\/p>\n<p><span style=\"color:#e6b223\">2.\u91cd\u5199compare\u65b9\u6cd5&#xff0c;\u8fd4\u56de\u503c\u4f9d\u65e7\u4e3a-1 0 1&#xff1b;<\/span><\/p>\n<p><span style=\"color:#e6b223\">3.\u521b\u5efa\u4e00\u4e2aTreeSet\u7c7b\u5bf9\u8c61\u65f6&#xff0c;\u5c06\u6bd4\u8f83\u5668\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6784\u9020\u51fd\u6570\u3002<\/span><\/p>\n<\/p>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u5206\u5f00\u6765\u770b\u8fd9\u4e09\u70b9\u3002<\/p>\n<p>\u7b2c\u4e00\u70b9\u548c\u7b2c\u4e8c\u70b9\u53ef\u4ee5\u7ed3\u5408\u8d77\u6765&#xff0c;\u521b\u5efa\u4e00\u4e2a\u5b9e\u73b0Comparator\u63a5\u53e3\u7684\u7c7b\u5e76\u5b9e\u73b0compare\u65b9\u6cd5\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u4fbf\u53ef\u4ee5\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\u7684\u77e5\u8bc6&#xff0c;\u7528\u533f\u540d\u5185\u90e8\u7c7b\u4ee3\u66ff\u63a5\u53e3\u7684\u529f\u80fd\u76f4\u63a5\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p>\u5fd8\u8bb0\u5185\u90e8\u7c7b\u77e5\u8bc6\u7684\u540c\u5b66\u53ef\u4ee5\u70b9\u51fb\u8df3\u8f6c\u5b66\u4e60\u4e00\u4e0b&#xff0c;\u5f88\u7b80\u5355\u3002<\/p>\n<p>\u533f\u540d\u5185\u90e8\u7c7b&#xff1a;Java\u7b2c\u5341\u8bfe \u5185\u90e8\u7c7b-CSDN\u535a\u5ba2<\/p>\n<\/p>\n<p>\u4ee3\u7801\u6f14\u793a&#xff1a;<\/p>\n<p>public class TestTree {<br \/>\n    public static void main(String[] args) {<br \/>\n        \/\/1.\u521b\u5efa\u4e00\u4e2a\u5b9e\u73b0Comparator\u63a5\u53e3\u7684\u5bf9\u8c61<br \/>\n        Comparator&lt;Student&gt; comparator&#061;new Comparator&lt;Student&gt;() {<br \/>\n            \/\/\u901a\u8fc7\u533f\u540d\u5185\u90e8\u7c7b\u5b9e\u73b0Comparator\u63a5\u53e3\u529f\u80fd<br \/>\n            \/\/2.\u91cd\u5199compara()<br \/>\n            &#064;Override<br \/>\n            public int compare(Student o1, Student o2) {<br \/>\n                int num;<br \/>\n                \/\/age\u5347\u5e8f<br \/>\n                num&#061;o1.getAge()-o2.getAge();<br \/>\n                if(num&#061;&#061;0){<br \/>\n                    \/\/name\u964d\u5e8f<br \/>\n                    num&#061; o2.getName().compareTo(o1.getName());<br \/>\n                    if(num&#061;&#061;0){<br \/>\n                        \/\/sex\u5347\u5e8f<br \/>\n                        num&#061;o1.getSex().compareTo(o2.getSex());<br \/>\n                    }<br \/>\n                }<br \/>\n                return num;<br \/>\n            }<\/p>\n<p>        };<br \/>\n   } <\/p>\n<p>\u7b2c\u4e09\u6b65\u521b\u5efa\u4e00\u4e2aTreeSet\u5bf9\u8c61&#xff0c;\u5c06\u6bd4\u8f83\u5668\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u8fdb\u53bb\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u521b\u5efa\u7684\u6bd4\u8f83\u5668\u53c2\u6570\u662fcomparator\u3002<\/p>\n<p>\u90a3\u4e48\u8bdd\u4e0d\u591a\u8bf4&#xff0c;\u76f4\u63a5\u4ee3\u7801\u6f14\u793a&#xff1a;<\/p>\n<p>        \/\/3.\u521b\u5efa\u4e00\u4e2aTreeSet\u7c7b\u5bf9\u8c61&#xff0c;\u5c06\u6bd4\u8f83\u5668\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6784\u9020\u51fd\u6570<br \/>\n        Set&lt;Student&gt; tree&#061;new TreeSet&lt;&gt;(comparator);<\/p>\n<p>        Student s1&#061;new Student(&#034;\u5f20\u9704&#034;,18,&#034;\u7537&#034;);<br \/>\n        Student s2&#061;new Student(&#034;\u513f\u9704&#034;,28,&#034;\u8d2d\u7269\u888b&#034;);<br \/>\n        Student s3&#061;new Student(&#034;\u5c0f\u9704&#034;,88,&#034;\u5973&#034;);<br \/>\n        tree.add(s1);<br \/>\n        tree.add(s2);<br \/>\n        tree.add(s3);<br \/>\n        System.out.println(tree);<\/p>\n<p><span style=\"color:#be191c\">Set&lt;Student&gt; tree&#061;new TreeSet&lt;&gt;(comparator);<\/span><\/p>\n<p>\u8fd9\u4e00\u6b65\u975e\u5e38\u5173\u952e\u4e00\u5b9a\u4e0d\u80fd\u5fd8\u8bb0&#xff01;&#xff01;&#xff01;<\/p>\n<\/p>\n<p>OK&#xff0c;\u5728\u5927\u5bb6\u4e86\u89e3\u4e86Comparator\u540e&#xff0c;\u60f3\u5fc5\u5bf9\u6211\u4eec\u521a\u624d\u7684\u4e24\u4e2a\u95ee\u9898\u4e5f\u6709\u4e86\u7b54\u6848\u3002<\/p>\n<p>&#8211;Comparator\u4fbf\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539Student\u6e90\u4ee3\u7801\u7684\u6761\u4ef6\u4e0b\u5b8c\u6210\u6392\u5e8f&#xff1b;<\/p>\n<p>&#8211;\u5e76\u4e14&#xff0c;\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u591a\u4e2aComparator\u5bf9\u8c61&#xff0c;\u6839\u636e\u4f20\u5165TreeSet\u7684\u53c2\u6570\u5bf9\u8c61\u4e0d\u540c&#xff0c;\u5b9e\u73b0\u4e0d\u540c\u7684\u6392\u5e8f\u3002<\/p>\n<p>\u6ce8\u610f&#xff1a;\u5982\u679c\u81ea\u7136\u6392\u5e8f\u548c\u6bd4\u8f83\u5668\u6392\u5e8f\u540c\u65f6\u5b58\u5728&#xff0c;\u6bd4\u8f83\u5668\u6392\u5e8f\u4f1a\u8986\u76d6\u6389\u81ea\u7136\u6392\u5e8f\u3002<\/p>\n<p>\u6bd5\u7adf&#xff0c;\u6211\u4eec\u4f20\u7684\u53c2\u6570\u53ef\u4e0d\u662f\u767d\u4f20\u7684&#xff0c;\u663e\u793a\u8c03\u7528\u6c38\u8fdc\u538b\u8fc7\u9690\u5f0f\u8c03\u7528\u4e00\u5934\u3002<\/p>\n<\/p>\n<p>\u76f8\u4fe1\u5927\u5bb6\u5df2\u7ecf\u638c\u63e1\u4e86\u6bd4\u8f83\u5668\u6392\u5e8f\u7684\u65b9\u6cd5&#xff0c;\u6211\u4eec\u6765\u8bd5\u7740\u7528\u4e00\u4e0b\u5427&#xff01;<\/p>\n<h4 id=\"2.2%20%E7%BB%83%E4%B9%A0%E4%BA%8C\" style=\"background-color:transparent\">2.2 \u7ec3\u4e60\u4e8c<\/h4>\n<p>\u7528\u6bd4\u8f83\u5668\u6392\u5e8f\u5b9e\u73b0&#xff1a;<\/p>\n<p>\u5c06\u6574\u65701~10\u5b9e\u73b0\u6392\u5e8f&#xff0c;\u8981\u6c42&#xff1a;<\/p>\n<p>&#8211;\u5947\u6570\u5728\u524d&#xff0c;\u5076\u6570\u5728\u540e&#xff1b;<\/p>\n<p>&#8211;\u5947\u6570\u5347\u5e8f&#xff0c;\u5076\u6570\u964d\u5e8f\u3002<\/p>\n<p>\u6700\u540e\u8f93\u51fa\u7ed3\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"54\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250813114452-689c7ab43d35e.png\" width=\"322\" \/><\/p>\n<\/p>\n<h3 id=\"%E4%B8%89%E3%80%81%E8%87%AA%E7%84%B6%E6%8E%92%E5%BA%8FVS%E6%AF%94%E8%BE%83%E5%99%A8%E6%8E%92%E5%BA%8F\">\u4e09\u3001\u81ea\u7136\u6392\u5e8fVS\u6bd4\u8f83\u5668\u6392\u5e8f<\/h3>\n<p>\u8fd9\u4e24\u8005\u7684\u533a\u522b\u5176\u5b9e\u86ee\u660e\u663e\u7684\u3002<\/p>\n<p><span style=\"color:#0d0016\">\u81ea\u7136\u6392\u5e8f&#xff1a;<\/span><\/p>\n<p><span style=\"color:#e6b223\">&#8211;\u63a5\u53e3&#xff1a;Comparable&#8211;\u91cd\u5199compareTo\u65b9\u6cd5&#xff1b;<\/span><\/p>\n<p><span style=\"color:#e6b223\">&#8211;\u91cd\u5199\u51fa\u6765\u7684\u65b9\u6cd5\u53ea\u6709\u4e00\u4e2a&#xff0c;\u6240\u4ee5\u6392\u5e8f\u89c4\u5219\u53ea\u6709\u4e00\u79cd&#xff1b;<\/span><\/p>\n<p><span style=\"color:#e6b223\">&#8211;\u5b9a\u4e49\u5728\u7c7b\u4e2d&#xff0c;\u5b9e\u73b0\u4ee3\u7801\u590d\u7528\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u6bd4\u8f83\u5668\u6392\u5e8f&#xff1a;<\/span><\/p>\n<p><span style=\"color:#e6b223\">&#8211;\u63a5\u53e3&#xff1a;Comparator&#8211;\u91cd\u5199compare\u65b9\u6cd5&#xff1b;<\/span><\/p>\n<p><span style=\"color:#e6b223\">&#8211;\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61&#xff0c;\u591a\u4e2a\u6392\u5e8f\u89c4\u5219&#xff1b;<\/span><\/p>\n<p><span style=\"color:#e6b223\">&#8211;\u4e0d\u4fee\u6539\u6e90\u7801&#xff0c;\u4f46\u662f\u6bcf\u6b21\u4f7f\u7528\u90fd\u9700\u8981\u5b9a\u4e49\u3002<\/span><\/p>\n<\/p>\n<h4 id=\"3.1%20%E7%BB%83%E4%B9%A0%E4%B8%89\">3.1 \u7ec3\u4e60\u4e09<\/h4>\n<p>\u5c06\u7ec3\u4e60\u4e00\u4e2d\u7684\u9898\u76ee\u7528\u6bd4\u8f83\u5668\u6392\u5e8f\u5b9e\u73b0\u3002<\/p>\n<p>\u57fa\u7840\u7c7b \u00a0 \u00a0 Student \u00a0 \u00a0 \u00a0 \u5c5e\u6027&#xff1a;name age score \u00a0 \u8981\u6c42 \u00a0 \u00a0 \u5c06\u5b66\u2f63\u6309\u7167\u59d3\u540d\u964d\u5e8f\u3001age\u5347\u5e8f&#xff0c;\u6210\u7ee9\u964d\u5e8f\u4fdd\u5b58\u5230\u96c6\u5408\u4e2d \u00a0 \u00a0 \u5e76\u4e14\u540d\u5b57\u53ebtom\u7684\u5b66\u2f63\u4e0d\u7ba1\u8003\u591a\u5c11\u5206\u90fd\u4f4d\u4e8e\u73ed\u7ea7\u7684\u7b2c\u2f00\u4f4d<\/p>\n<\/p>\n<h3 id=\"%E5%9B%9B%E3%80%81%E4%B9%A0%E9%A2%98%E7%AD%94%E6%A1%88\">\u56db\u3001\u4e60\u9898\u7b54\u6848<\/h3>\n<h4 id=\"4.1%20%E7%BB%83%E4%B9%A0%E4%B8%80\">4.1 \u7ec3\u4e60\u4e00<\/h4>\n<p>\u57fa\u7840\u7c7b \u00a0 \u00a0 Student \u00a0 \u00a0 \u00a0 \u5c5e\u6027&#xff1a;name age score \u00a0 \u8981\u6c42 \u00a0 \u00a0 \u5c06\u5b66\u2f63\u6309\u7167\u59d3\u540d\u964d\u5e8f\u3001age\u5347\u5e8f&#xff0c;\u6210\u7ee9\u964d\u5e8f\u4fdd\u5b58\u5230\u96c6\u5408\u4e2d \u00a0 \u00a0 \u5e76\u4e14\u540d\u5b57\u53ebtom\u7684\u5b66\u2f63\u4e0d\u7ba1\u8003\u591a\u5c11\u5206\u90fd\u4f4d\u4e8e\u73ed\u7ea7\u7684\u7b2c\u2f00\u4f4d<\/p>\n<p>Student\u7c7b&#xff1a;<\/p>\n<p>\/\/\u81ea\u7136\u6392\u5e8f<br \/>\n    &#064;Override<br \/>\n    public int compareTo(Student o) {<br \/>\n        int num;<br \/>\n        if(this.name&#061;&#061;&#034;tom&#034;){return -1;}<br \/>\n        \/\/name\u964d\u5e8f<br \/>\n        num&#061;o.name.compareTo(this.name);<br \/>\n        if(num&#061;&#061;0){<br \/>\n            \/\/age\u5347\u5e8f<br \/>\n            num&#061;this.age-o.age;<br \/>\n            if(num&#061;&#061;0){<br \/>\n                \/\/score\u964d\u5e8f<br \/>\n                num&#061;o.score-this.score;<br \/>\n            }<br \/>\n        }<br \/>\n        return num;<br \/>\n    } <\/p>\n<p>Test\u7c7b&#xff1a;<\/p>\n<p>ublic class Test {<br \/>\n    public static void main(String[] args) {<\/p>\n<p>        Student s1&#061;new Student(&#034;\u9648\u9704&#034;,18,99);<br \/>\n        Student s2&#061;new Student(&#034;\u9648\u9704&#034;,168,989);<br \/>\n        Student s3&#061;new Student(&#034;\u5973\u9704&#034;,28,999);<br \/>\n        Student s4&#061;new Student(&#034;tom&#034;,17,199);<br \/>\nSystem.out.println(&#034;\u81ea\u7136\u6392\u5e8f&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#034;);<br \/>\n        \/\/\u81ea\u7136\u6392\u5e8f<br \/>\n        Set&lt;Student&gt; tree1&#061;new TreeSet&lt;&gt;();<br \/>\n        tree1.add(s1);<br \/>\n        tree1.add(s2);<br \/>\n        tree1.add(s3);<br \/>\n        tree1.add(s4);<br \/>\n        System.out.println(tree1);}} <\/p>\n<h4 id=\"4.2%20%E7%BB%83%E4%B9%A0%E4%BA%8C\">4.2 \u7ec3\u4e60\u4e8c<\/h4>\n<p>\u5c06\u6574\u65701~10\u5b9e\u73b0\u6392\u5e8f&#xff0c;\u8981\u6c42&#xff1a;<\/p>\n<p>&#8211;\u5947\u6570\u5728\u524d&#xff0c;\u5076\u6570\u5728\u540e&#xff1b;<\/p>\n<p>&#8211;\u5947\u6570\u5347\u5e8f&#xff0c;\u5076\u6570\u964d\u5e8f\u3002<\/p>\n<p>public class Test {<br \/>\n    public static void main(String[] args) {<br \/>\n         Comparator&lt;Integer&gt; comparator&#061;new Comparator&lt;Integer&gt;(){<br \/>\n            &#064;Override<br \/>\n            public int compare(Integer o1, Integer o2){<br \/>\n                int num;<br \/>\n                if(o1%2!&#061;0){ if(o2%2!&#061;0){num&#061;o1.compareTo(o2); return num; }   return -1;}<br \/>\n                else { if(o2%2&#061;&#061;0){num&#061;o2.compareTo(o1);return num;} return 1;}<br \/>\n            }<br \/>\n         };<\/p>\n<p>        Set&lt;Integer&gt; tree&#061;new TreeSet&lt;&gt;(comparator);<br \/>\n        for(int i&#061;1;i&lt;&#061;10;i&#043;&#043;){<br \/>\n            tree.add(i);<br \/>\n        }<br \/>\n        System.out.println(tree);<\/p>\n<p>    }<br \/>\n} <\/p>\n<h4 id=\"4.3%20%E7%BB%83%E4%B9%A0%E4%B8%89\">4.3 \u7ec3\u4e60\u4e09<\/h4>\n<p>\u6bd4\u8f83\u5668\u6392\u5e8f\u5b9e\u73b0\u7ec3\u4e60\u4e00&#xff1a;<\/p>\n<p>\u57fa\u7840\u7c7b \u00a0 \u00a0 Student \u00a0 \u00a0 \u00a0 \u5c5e\u6027&#xff1a;name age score \u00a0 \u8981\u6c42 \u00a0 \u00a0 \u5c06\u5b66\u2f63\u6309\u7167\u59d3\u540d\u964d\u5e8f\u3001age\u5347\u5e8f&#xff0c;\u6210\u7ee9\u964d\u5e8f\u4fdd\u5b58\u5230\u96c6\u5408\u4e2d \u00a0 \u00a0 \u5e76\u4e14\u540d\u5b57\u53ebtom\u7684\u5b66\u2f63\u4e0d\u7ba1\u8003\u591a\u5c11\u5206\u90fd\u4f4d\u4e8e\u73ed\u7ea7\u7684\u7b2c\u2f00\u4f4d<\/p>\n<p>        \/\/\u6bd4\u8f83\u5668\u6392\u5e8f<br \/>\n        Comparator&lt;Student&gt; comparator&#061;new Comparator&lt;Student&gt;() {<\/p>\n<p>        &#064;Override<br \/>\n        public int compare(Student o1, Student o2) {<br \/>\n            int num;<br \/>\n            if(o1.getName()&#061;&#061;&#034;tom&#034;){return -1;}<br \/>\n            \/\/name\u964d\u5e8f<br \/>\n            num&#061;o2.getName().compareTo(o1.getName());<br \/>\n            if(num&#061;&#061;0){<br \/>\n                \/\/age\u5347\u5e8f<br \/>\n                num&#061;o1.getAge()-o2.getAge();<br \/>\n                if(num&#061;&#061;0){<br \/>\n                    \/\/score\u964d\u5e8f<br \/>\n                    num&#061;o2.getScore()-o1.getScore();<br \/>\n                 }<br \/>\n            }<br \/>\n        return num;<br \/>\n            }<br \/>\n        };<br \/>\n        Set&lt;Student&gt; tree2&#061;new TreeSet&lt;&gt;(comparator);<br \/>\n        tree2.add(s1);<br \/>\n        tree2.add(s2);<br \/>\n        tree2.add(s3);<br \/>\n        tree2.add(s4);<br \/>\n        System.out.println(tree2); <\/p>\n<\/p>\n<h3 id=\"%E4%BA%94%E3%80%81%E6%89%A9%E5%B1%95\">\u4e94\u3001\u6269\u5c55<\/h3>\n<p>\u8fd9\u91cc\u6574\u7406\u4e00\u4e0b\u4e0d\u540c\u96c6\u5408\u7684\u6392\u5e8f\u89c4\u5219\u3002<\/p>\n<p>\u6211\u4eec\u76ee\u524d\u5b66\u4e86ArrayList&#xff0c;LinkedList&#xff0c;HashSet&#xff0c;LinkedHashSet&#xff0c;TreeSet\u3002<\/p>\n<\/p>\n<table>\n<tr>\u96c6\u5408\u7c7b\u578b\u6392\u5e8f\u65b9\u5f0f\u6392\u5e8f\u4f9d\u636e \/ \u89c4\u5219<\/tr>\n<tbody>\n<tr>\n<td>ArrayList<\/td>\n<td>\u9700\u624b\u52a8\u6392\u5e8f<\/td>\n<td><span style=\"color:#1c7331\">\u63d2\u5165\u987a\u5e8f&#xff08;\u9ed8\u8ba4&#xff09;<\/span>&#xff1b;Collections.sort()\u81ea\u5b9a\u4e49<\/td>\n<\/tr>\n<tr>\n<td>LinkedList<\/td>\n<td>\u9700\u624b\u52a8\u6392\u5e8f<\/td>\n<td><span style=\"color:#1c7331\">\u63d2\u5165\u987a\u5e8f&#xff08;\u9ed8\u8ba4&#xff09;<\/span>&#xff1b;Collections.sort()\u81ea\u5b9a\u4e49<\/td>\n<\/tr>\n<tr>\n<td>HashSet<\/td>\n<td>\u4e0d\u6392\u5e8f<\/td>\n<td><span style=\"color:#1a439c\">\u65e0\u5e8f&#xff08;\u54c8\u5e0c\u8868\u5b9e\u73b0&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td>LinkedHashSet<\/td>\n<td>\u56fa\u5b9a\u6392\u5e8f<\/td>\n<td><span style=\"color:#1c7331\">\u63d2\u5165\u987a\u5e8f<\/span>&#xff08;\u94fe\u8868\u7ef4\u62a4&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>TreeSet<\/td>\n<td>\u81ea\u52a8\u6392\u5e8f<\/td>\n<td><span style=\"color:#e6b223\">\u81ea\u7136\u6392\u5e8f&#xff08;Comparable&#xff09;\u6216\u6bd4\u8f83\u5668&#xff08;Comparator&#xff09;<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5927\u5bb6\u4e5f\u53ef\u4ee5\u770b\u51fa\u6765&#xff0c;\u6211\u4eec\u7684\u6392\u5e8f\u57fa\u672c\u5206\u4e3a\u4e09\u79cd\u3002<\/p>\n<\/p>\n<p><span style=\"color:#e6b223\">\u901a\u8fc7\u7406\u89e3\u8fd9\u4e9b\u89c4\u5219&#xff0c;\u53ef\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u96c6\u5408&#xff1a;\u9700\u987a\u5e8f\u7528LinkedHashSet&#xff0c;\u9700\u6392\u5e8f\u7528TreeSet&#xff0c;\u4ec5\u53bb\u91cd\u65e0\u5e8f\u7528HashSet&#xff0c;\u52a8\u6001\u5217\u8868\u7528ArrayList\/LinkedList\u3002<\/span><\/p>\n<\/p>\n<p>\u5f53\u7136&#xff0c;\u6211\u4eec\u540e\u7eed\u8fd8\u4f1a\u5b66\u4e60Map&#xff0c;\u6709\u4e86Map\u7684\u52a0\u5165&#xff0c;\u6211\u4eec\u7684\u9009\u62e9\u53c8\u4f1a\u591a\u8d77\u6765\u3002<\/p>\n<p>OK&#xff0c;\u8fd9\u7bc7\u6587\u7ae0\u4fbf\u5230\u6b64\u7ed3\u675f&#xff01;<\/p>\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"137\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250813114452-689c7ab4c87f1.png\" width=\"149\" \/><\/p>\n<p>\u661f\u8fb0\u4ea6\u6709\u8fdc\u65b9&#xff1b;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u5f7c\u65f6\u7684\u4f60\u53c8\u5fc3\u5411\u8c01\u5462&#xff1f;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4\u6b21\u3002\u672c\u6587\u6df1\u5165\u63a2\u8ba8\u4e86Java\u4e2dTreeSet\u7684\u4e24\u79cd\u6392\u5e8f\u65b9\u5f0f\uff1a\u81ea\u7136\u6392\u5e8f\u548c\u6bd4\u8f83\u5668\u6392\u5e8f\u3002\u81ea\u7136\u6392\u5e8f\u901a\u8fc7\u5b9e\u73b0Comparable\u63a5\u53e3\u5e76\u91cd\u5199compareTo()\u65b9\u6cd5\u5b9e\u73b0\uff0c\u9002\u7528\u4e8e\u81ea\u5b9a\u4e49\u7c7b\u6392\u5e8f\uff1b\u6bd4\u8f83\u5668\u6392\u5e8f\u5219\u901a\u8fc7Comparator\u63a5\u53e3\u5b9e\u73b0\uff0c\u5728\u4e0d\u4fee\u6539\u6e90\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u652f\u6301\u591a\u79cd\u6392\u5e8f\u89c4\u5219\u3002\u6587\u7ae0\u8be6\u7ec6\u6f14\u793a\u4e86\u4e24\u79cd\u6392\u5e8f\u65b9\u5f0f\u5728Integer\u3001String\u548c\u81ea\u5b9a\u4e49\u7c7b\u4e2d\u7684\u5e94\u7528\uff0c\u5bf9\u6bd4\u4e86\u4e24\u8005\u7684\u7279\u70b9\uff08\u81ea\u7136\u6392\u5e8f\u4ee3\u7801\u590d\u7528\u4f46\u89c4\u5219\u5355\u4e00\uff0c\u6bd4\u8f83\u5668\u6392\u5e8f\u7075\u6d3b\u4f46\u9700\u91cd\u590d\u5b9a\u4e49\uff09\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e09\u4e2a\u5178\u578b\u7ec3\u4e60\u53ca\u5176\u5b8c\u6574\u89e3\u7b54\u3002\u6700\u540e\u603b\u7ed3\u4e86\u4e0d\u540c\u96c6\u5408\u7c7b\u7684\u6392\u5e8f\u7279\u6027\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u96c6<\/p>\n","protected":false},"author":2,"featured_media":55231,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5677,5676,5679,5678,5675,5682,5681,5680],"topic":[],"class_list":["post-55233","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-comparable","tag-comparator","tag-compare","tag-compareto","tag-java","tag-treeset","tag-5681","tag-5680"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u81ea\u7136\u6392\u5e8fVS\u6bd4\u8f83\u5668\u6392\u5e8f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/55233.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u81ea\u7136\u6392\u5e8fVS\u6bd4\u8f83\u5668\u6392\u5e8f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4\u6b21\u3002\u672c\u6587\u6df1\u5165\u63a2\u8ba8\u4e86Java\u4e2dTreeSet\u7684\u4e24\u79cd\u6392\u5e8f\u65b9\u5f0f\uff1a\u81ea\u7136\u6392\u5e8f\u548c\u6bd4\u8f83\u5668\u6392\u5e8f\u3002\u81ea\u7136\u6392\u5e8f\u901a\u8fc7\u5b9e\u73b0Comparable\u63a5\u53e3\u5e76\u91cd\u5199compareTo()\u65b9\u6cd5\u5b9e\u73b0\uff0c\u9002\u7528\u4e8e\u81ea\u5b9a\u4e49\u7c7b\u6392\u5e8f\uff1b\u6bd4\u8f83\u5668\u6392\u5e8f\u5219\u901a\u8fc7Comparator\u63a5\u53e3\u5b9e\u73b0\uff0c\u5728\u4e0d\u4fee\u6539\u6e90\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u652f\u6301\u591a\u79cd\u6392\u5e8f\u89c4\u5219\u3002\u6587\u7ae0\u8be6\u7ec6\u6f14\u793a\u4e86\u4e24\u79cd\u6392\u5e8f\u65b9\u5f0f\u5728Integer\u3001String\u548c\u81ea\u5b9a\u4e49\u7c7b\u4e2d\u7684\u5e94\u7528\uff0c\u5bf9\u6bd4\u4e86\u4e24\u8005\u7684\u7279\u70b9\uff08\u81ea\u7136\u6392\u5e8f\u4ee3\u7801\u590d\u7528\u4f46\u89c4\u5219\u5355\u4e00\uff0c\u6bd4\u8f83\u5668\u6392\u5e8f\u7075\u6d3b\u4f46\u9700\u91cd\u590d\u5b9a\u4e49\uff09\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e09\u4e2a\u5178\u578b\u7ec3\u4e60\u53ca\u5176\u5b8c\u6574\u89e3\u7b54\u3002\u6700\u540e\u603b\u7ed3\u4e86\u4e0d\u540c\u96c6\u5408\u7c7b\u7684\u6392\u5e8f\u7279\u6027\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u96c6\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/55233.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-13T11:44:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250813114452-689c7ab43d35e.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/55233.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/55233.html\",\"name\":\"\u81ea\u7136\u6392\u5e8fVS\u6bd4\u8f83\u5668\u6392\u5e8f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-13T11:44:54+00:00\",\"dateModified\":\"2025-08-13T11:44:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/55233.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/55233.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/55233.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u81ea\u7136\u6392\u5e8fVS\u6bd4\u8f83\u5668\u6392\u5e8f\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u81ea\u7136\u6392\u5e8fVS\u6bd4\u8f83\u5668\u6392\u5e8f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/55233.html","og_locale":"zh_CN","og_type":"article","og_title":"\u81ea\u7136\u6392\u5e8fVS\u6bd4\u8f83\u5668\u6392\u5e8f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4\u6b21\u3002\u672c\u6587\u6df1\u5165\u63a2\u8ba8\u4e86Java\u4e2dTreeSet\u7684\u4e24\u79cd\u6392\u5e8f\u65b9\u5f0f\uff1a\u81ea\u7136\u6392\u5e8f\u548c\u6bd4\u8f83\u5668\u6392\u5e8f\u3002\u81ea\u7136\u6392\u5e8f\u901a\u8fc7\u5b9e\u73b0Comparable\u63a5\u53e3\u5e76\u91cd\u5199compareTo()\u65b9\u6cd5\u5b9e\u73b0\uff0c\u9002\u7528\u4e8e\u81ea\u5b9a\u4e49\u7c7b\u6392\u5e8f\uff1b\u6bd4\u8f83\u5668\u6392\u5e8f\u5219\u901a\u8fc7Comparator\u63a5\u53e3\u5b9e\u73b0\uff0c\u5728\u4e0d\u4fee\u6539\u6e90\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u652f\u6301\u591a\u79cd\u6392\u5e8f\u89c4\u5219\u3002\u6587\u7ae0\u8be6\u7ec6\u6f14\u793a\u4e86\u4e24\u79cd\u6392\u5e8f\u65b9\u5f0f\u5728Integer\u3001String\u548c\u81ea\u5b9a\u4e49\u7c7b\u4e2d\u7684\u5e94\u7528\uff0c\u5bf9\u6bd4\u4e86\u4e24\u8005\u7684\u7279\u70b9\uff08\u81ea\u7136\u6392\u5e8f\u4ee3\u7801\u590d\u7528\u4f46\u89c4\u5219\u5355\u4e00\uff0c\u6bd4\u8f83\u5668\u6392\u5e8f\u7075\u6d3b\u4f46\u9700\u91cd\u590d\u5b9a\u4e49\uff09\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e09\u4e2a\u5178\u578b\u7ec3\u4e60\u53ca\u5176\u5b8c\u6574\u89e3\u7b54\u3002\u6700\u540e\u603b\u7ed3\u4e86\u4e0d\u540c\u96c6\u5408\u7c7b\u7684\u6392\u5e8f\u7279\u6027\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u96c6","og_url":"https:\/\/www.wsisp.com\/helps\/55233.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-13T11:44:54+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250813114452-689c7ab43d35e.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/55233.html","url":"https:\/\/www.wsisp.com\/helps\/55233.html","name":"\u81ea\u7136\u6392\u5e8fVS\u6bd4\u8f83\u5668\u6392\u5e8f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-13T11:44:54+00:00","dateModified":"2025-08-13T11:44:54+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/55233.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/55233.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/55233.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u81ea\u7136\u6392\u5e8fVS\u6bd4\u8f83\u5668\u6392\u5e8f"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/55233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=55233"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/55233\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/55231"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=55233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=55233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=55233"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=55233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}