TreeSet和HashSet的问题

来源:互联网 发布:游戏编程 知乎 编辑:程序博客网 时间:2024/05/17 05:06
1.TreeSet集合中自然排序和自定义比较规则,什么时候需要实现Comparable????        1)当存储进集合中的对象实现Comparable则可以使用自然排序规则,重写compareTo()方法        public int compareTo() {            return 0;        }        例如 :        public int compareTo(Teacher o) {            /*int num = this.age - o.age;            int result = num==0 ? this.name.compareTo(o.name) : num;            return result;*/            //从小到大排序            int age = this.age - o.age;            int name = this.name.compareTo(o.name);            if(age != 0) {                return age;            }            else if(name != 0) {                return name;            }            else {                return 0;            }        }        2)当存储进集合中对象没有实现Comparable则可以通过自定义比较规则进行比较(可以采用匿名内部类的形式)        TreeSet<T> ts = new TreeSet<T> (                new Comparator<T> () {                    public int compare(T O1, T O2) {                        return 0;                    }                }                );        例如 :        TreeSet<Teacher1> ts1 = new TreeSet<Teacher1>(                new Comparator<Teacher1>() {                    @Override                    public int compare(Teacher1 o1, Teacher1 o2) {                        // TODO Auto-generated method stub                        //o1 - o2 表示从小到大的顺序,反之从大到小排序                        int age = o1.age - o2.age;                        int name = o1.name.compareTo(o2.name);                        return age == 0 ? name : age;                }            });2.HashSet集合添加方法add()底层调用了equals()方法和 hashCode()方法,所以自定义类添加到集合,需要重写上述两个方法                例如 :        public class Test2 {            public static void main(String[] args) {                Teacher t1 = new Teacher(1, "fg");                Teacher t2 = new Teacher(1, "fg");                Set<Teacher> set = new HashSet<Teacher>();                System.out.println(set.add(t1));                System.out.println(set.add(t2));            }        }        class Teacher{            int age;            String name;            public Teacher(int age, String name) {                super();                this.age = age;                this.name = name;            }            @Override            public int hashCode() {                final int prime = 31;                int result = 1;                result = prime * result + age;                result = prime * result + ((name == null) ? 0 : name.hashCode());                return result;            }            @Override            public boolean equals(Object obj) {                if (this == obj)                    return true;                if (obj == null)                    return false;                if (getClass() != obj.getClass())                    return false;                Teacher other = (Teacher) obj;                if (age != other.age)                    return false;                if (name == null) {                    if (other.name != null)                        return false;                } else if (!name.equals(other.name))                    return false;                return true;            }           }------------------------------------------------------------------      3.TreeSet源码解析??//创建TreeSet集合TreeSet<Integer> ts = new TreeSet<Integer>();//添加元素到集合ts.add(20);ts.add(18);ts.add(23);ts.add(22);ts.add(17);ts.add(24);ts.add(19);//重复元素ts.add(18);ts.add(24);结果: 17 18 19 20 22 23 24------------------------------------源代码:class TreeSet {    public boolean add(E e) {        return m.put(e, PRESENT)==null;    }}class TreeMap {    public V put(K key, V value) {        //key -- e -- 20 -- 18        Entry<K,V> t = root;        if (t == null) {            compare(key, key); // type (and possibly null) check            root = new Entry<>(key, value, null);            size = 1;            modCount++;            return null;        }        int cmp;        Entry<K,V> parent;        // split comparator and comparable paths        Comparator<? super K> cpr = comparator;        if (cpr != null) {            // 采用比较器的方式            do {                parent = t;                cmp = cpr.compare(key, t.key);                if (cmp < 0)                    t = t.left;                else if (cmp > 0)                    t = t.right;                else                    return t.setValue(value);            } while (t != null);        }        else {            //采用自然排序方式            if (key == null)                throw new NullPointerException();            Comparable<? super K> k = (Comparable<? super K>) key;            //k -- 18            do {                parent = t;                cmp = k.compareTo(t.key);                if (cmp < 0)                    t = t.left;                else if (cmp > 0)                    t = t.right;                else                    return t.setValue(value);            } while (t != null);        }        Entry<K,V> e = new Entry<>(key, value, parent);        if (cmp < 0)            parent.left = e;        else            parent.right = e;        fixAfterInsertion(e);        size++;        modCount++;        return null;    }}