A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 飘落 中级黑马   /  2013-9-23 07:53  /  1284 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 飘落 于 2013-9-23 22:51 编辑

Comparator作为一个接口,有两个方法:compare()和equals(),
为什么只重写compare(),编译不报错,还能正常运行,
难道equals()不是抽象方法,不用重写?
求解释。

评分

参与人数 1技术分 +1 收起 理由
曹秀云 + 1 神马都是浮云

查看全部评分

5 个回复

倒序浏览
原因是被Object类的equals覆写了。
回复 使用道具 举报
因为Object类已经覆写了equals方法了,上次写了个例子,给楼主参考参考,下面是源代码--->

package yting.collection;

import java.util.*;

public class TreeSetDemo1 {
        public static void main(String[] args) {
                TreeSet<Student> ts = new TreeSet<Student>();
                //TreeSet<Student> ts = new TreeSet<Student>(new strLenComparator());   //想看效果把这行代码注释去掉
                                                                                                                                                                //把上面那行代码注释掉
                ts.add(new Student("lisi001", 11));
                ts.add(new Student("lisi005", 15));
                ts.add(new Student("lisi003", 13));
                ts.add(new Student("lisi004", 14));
                ts.add(new Student("lisi002", 12));
                ts.add(new Student("lisi006", 12));
               
                Iterator<Student> it = ts.iterator();
                while(it.hasNext()) {
                        Student stu = (Student)it.next();
                        System.out.println(stu.getName()+"..."+stu.getAge());
                }
        }
}


class Student implements Comparable<Student> {
        private String name;
        private int age;
       
        public Student(String name, int age) {
                super();
                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(Student stu) {
                //这里是根据name排序的
                return this.name.compareTo(stu.getName());
        }
}

//这个比较器是根据age排序的
class strLenComparator implements Comparator<Student> {

        @Override
        public int compare(Student stu1, Student stu2) {
                return (stu1.getAge() - stu2.getAge())==0 ?  stu1.getName().compareTo(stu2.getName()) : stu1.getAge() - stu2.getAge();
        }
}

希望对楼主有帮助、、、

The you smile until forever 、、、、、、、、、、、、、、、、、、、、、

评分

参与人数 1技术分 +1 收起 理由
黄文伯 + 1 很给力!

查看全部评分

回复 使用道具 举报
equalsboolean equals(Object obj)指示某个其他对象是否“等于”此 Comparator。此方法必须遵守 Object.equals(Object) 的常规协定。此外,仅当 指定的对象也是一个 Comparator,并且强行实施与此 Comparator 相同的排序时,此方法才返回 true。因此,comp1.equals(comp2) 意味着对于每个对象引用 o1 和 o2 而言,都存在 sgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2))。注意, 重写 Object.equals(Object) 方法总是 安全的。然而,在某些情况下,重写此方法可以允许程序确定两个不同的 Comparator 是否强行实施了相同的排序,从而提高性能。
这个是API中提供的解释,我 觉得有时候看看API文档还是挺有用的。


评分

参与人数 1技术分 +1 收起 理由
黄文伯 + 1 25分!撒花撒花!

查看全部评分

回复 使用道具 举报
亲,如问题已解决请将分类的“未解决”改为“已解决”。
以后的问题贴也要及时更改分类哦~
回复 使用道具 举报
黄文伯 发表于 2013-9-23 18:37
亲,如问题已解决请将分类的“未解决”改为“已解决”。
以后的问题贴也要及时更改分类哦~ ...

楼主把这个家伙扔出去{:soso_e199:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马