[java集合]comparable与comparator

/**
 * comparable
 接口在类设计中就应该由类去实现,并实现compareTo方法。
 接口位于java.lang包下。
 对比方法叫compareTo

 comparator
 接口在类已经完成之后还想再要比较。
 接口位于java.util包下
 对比方法叫compare
 */

TreeSet<TestChild> treeSet = new TreeSet<>();
treeSet.add(new TestChild("java02",2));
treeSet.add(new TestChild("java03",3));
treeSet.add(new TestChild("java01",1));
treeSet.add(new TestChild("java01",2));
/**
 * 输出
 java01........1
 java01........2
 java02........2
 java03........3
 */
Iterator<TestChild> iterator = treeSet.iterator();
while(iterator.hasNext()){
    TestChild testChild = iterator.next();
    System.out.println(testChild.getName() +"........" + testChild.getAge());
}
System.out.println("===================我是分割线==================");

//treeSet如果有比较器则优先使用比较器排序,否则用实现的comparable接口中的compareTo方法比较。

TreeSet<TestChild> treeSet1 = new TreeSet<>(new TestChildComparator());
treeSet1.add(new TestChild("java02",2));
treeSet1.add(new TestChild("java03",3));
treeSet1.add(new TestChild("java01",1));
treeSet1.add(new TestChild("java01",2));
iterator = treeSet1.iterator();
/**
 treeSet1.add(new TestChild("java01",2));
 失败的原因是比较器年龄相同返回0,hashSet看成相同元素,不添加。
 输出
 java03........3
 java02........2
 java01........1
 */
while(iterator.hasNext()){
    TestChild testChild = iterator.next();
    System.out.println(testChild.getName() +"........" + testChild.getAge());
}

System.out.println("===================我是分割线之二==================");
TreeSet<TestChild> treeSet2 = new TreeSet<>();
treeSet2.add(new TestChild("java02",2));
treeSet2.add(new TestChild("java03",3));
treeSet2.add(new TestChild("java01",1));
treeSet2.add(new TestChild("java01",2));

TestChild[] aaa = new TestChild[treeSet2.size()];

Arrays.sort(treeSet2.toArray(aaa),new TestChildComparator());
iterator = treeSet2.iterator();
/**
 添加成功 treeSet2.add(new TestChild("java01",2));
 因为一开始么有用比较器,所以添加成功。
 输出
 java01........1
 java01........2
 java02........2
 java03........3
 */
while(iterator.hasNext()){
    TestChild testChild = iterator.next();
    System.out.println(testChild.getName() +"........" + testChild.getAge());
}

System.out.println("===================我是分割线之三==================");
/**
 输出
 java03........3
 java01........2
 java02........2
 java01........1
 */
for (int i = 0 ; i < aaa.length ; i ++){
    System.out.println(aaa[i].getName() +"........" + aaa[i].getAge());

}

private static class TestChildComparator implements Comparator<TestChild>{

    @Override
    public int compare(TestChild o1, TestChild o2) {
        if(o1.getAge() > o2.getAge()){
            return -1;
        }else if(o1.getAge() == o2.getAge()){
            return 0;
        }
        return 1;
    }
}

private static class TestChild implements Comparable{

    private String name;

    private int age;

    public TestChild(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(Object o) {

        TestChild testChild = (TestChild)o;

        if(age > testChild.getAge()){
            return 1;
        }else if(age == testChild.getAge()){
            return name.compareTo(testChild.getName());
        }
        return -1;

    }

    @Override
    public boolean equals(Object obj) {

        TestChild testChild = (TestChild)obj;
        return testChild.getAge() == testChild.getAge();

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值