Jeffrey.pace 2022-03-20 16:43 采纳率: 64.7%
浏览 87
已结题

这段代码,为什么用TreeSet自然排序后,和用HashSet呈现不一样的结果

我的理解是:TreeSet可以进行自定义排序,但是内容应该和选用HashSet是一样的。但是结果是:使用TreeSet打印集合少了一个(我认为少了个元素[student 2020124 is 19 years old])。是不是和我写的compareTo方法有关系?

使用HashSet:

import java.util.HashSet;
import java.util.TreeSet;

class testTre{
    public int age;
    public final String id;
    public testTre(String id, int age){
        this.id = id;
        this.age = age;
    }
    @Override
    public String toString(){
        return "[student " + this.id + " is "+ this.age + " years old]";
    }
//    @Override
//    public int compareTo(Object obj){      //  自然排序
//        testTre temp = (testTre) obj;
////        if(this.age < temp.age)
////            return -1;
////        else
////            return 1;
//        if(temp.age == this.age)
//            System.out.println("相同");
//        return this.age > temp.age ? 1 : this.age < temp.age ? -1 : 0;
//    }
    @Override
    public boolean equals(Object obj){
        if(this == obj){   // 判断是否为同一个对象,即自己与自己比较
            return true;
        }
        else if( obj != null && obj.getClass() == testTre.class){  // 判断两个对象是否出自同一个(运行时)类
            testTre temp = (testTre) obj; // 强制类型转换
            return temp.id == this.id;
        }
        return false;
    }
    @Override
    public int hashCode(){
        return Integer.valueOf(this.id).intValue();
    }
}
public class Treeset {
    public static void main(String[] args){
        HashSet Ts = new HashSet();
        Ts.add(new testTre("2020106",12));
        Ts.add(new testTre("2020108",19));
        Ts.add(new testTre("2020208",15));
        Ts.add(new testTre("2020104",22));
        Ts.add(new testTre("2020124",19));
        Ts.add(new testTre("2020124",19));

        System.out.println(Ts);
    }
}
// HashSet:
//[[student 2020108 is 19 years old], [student 2020124 is 19 years old],
// [student 2020106 is 12 years old], [student 2020104 is 22 years old],
// [student 2020208 is 15 years old]]

使用TreeSet:

import java.util.HashSet;
import java.util.TreeSet;

class testTre implements Comparable{
    public int age;
    public final String id;
    public testTre(String id, int age){
        this.id = id;
        this.age = age;
    }
    @Override
    public String toString(){
        return "[student " + this.id + " is "+ this.age + " years old]";
    }
    @Override
    public int compareTo(Object obj){      //  自然排序
        testTre temp = (testTre) obj;
//        if(this.age < temp.age)
//            return -1;
//        else
//            return 1;
//        if(temp.age == this.age)
//            System.out.println("相同");
        return this.age > temp.age ? 1 : this.age < temp.age ? -1 : 0;
    }
    @Override
    public boolean equals(Object obj){
        if(this == obj){   // 判断是否为同一个对象,即自己与自己比较
            return true;
        }
        else if( obj != null && obj.getClass() == testTre.class){  // 判断两个对象是否出自同一个(运行时)类
            testTre temp = (testTre) obj; // 强制类型转换
            return temp.id == this.id;
        }
        return false;
    }
    @Override
    public int hashCode(){
        return Integer.valueOf(this.id).intValue();
    }
}
public class Treeset {
    public static void main(String[] args){
        TreeSet Ts = new TreeSet();
        Ts.add(new testTre("2020106",12));
        Ts.add(new testTre("2020108",19));
        Ts.add(new testTre("2020208",15));
        Ts.add(new testTre("2020104",22));
        Ts.add(new testTre("2020124",19));
        Ts.add(new testTre("2020124",19));

        System.out.println(Ts);
    }
}
// TreeSet:
// [[student 2020106 is 12 years old], [student 2020208 is 15 years old],
//  [student 2020108 is 19 years old], [student 2020104 is 22 years old]]

  • 写回答

1条回答 默认 最新

  • heling_m6ss 2022-03-20 19:03
    关注

    treeSet 和 HashSet 存放的元素都是唯一的, HashSet 是根据equals 和 hashCode 来判断元素是否重读 而 treeSet 是根据compareTo 来判断 大于0 放左边小于0 放右边 等于0 判断为相同忽略

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月29日
  • 已采纳回答 3月21日
  • 创建了问题 3月20日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改