日期:2014-05-20 浏览次数:21214 次
Given:
1. public class Drink implements Comparable {
2. public String name;
3. public int compareTo(Object o) {
4. return 0;
5. }
6. }
and:
20. Drink one = new Drink();
21. Drink two = new Drink();
22. one.name= "Coffee";
23. two.name= "Tea";
24. TreeSet set = new TreeSet();
25. set.add(one);
26. set.add(two);
    public V put(K key, V value) {
        Entry<K,V> t = root;
        if (t == null) {
            incrementSize();
            root = new Entry<K,V>(key, value, null);
            return null;
       }
        while (true) {
            int cmp = compare(key, t.key);
            if (cmp == 0) {
                return t.setValue(value);
            } else if (cmp < 0) {
                if (t.left != null) {
                    t = t.left;
                } else {
                    incrementSize();
                    t.left = new Entry<K,V>(key, value, t);
                    fixAfterInsertion(t.left);
                    return null;
                }
            } else { // cmp > 0
                if (t.right != null) {
                    t = t.right;
                } else {
                    incrementSize();
                    t.right = new Entry<K,V>(key, value, t);
                    fixAfterInsertion(t.right);
                    return null;
                }
            }
        }
    }
------解决方案--------------------
hashCode是对于HashSet的,跟这个TreeSet没有任何关系。
你这个Drink类之所以实现了Comparable就是给TreeSet进行比较用的,你的类实现了compareTo方法后就跟自然比较没关系了。TreeSet 实例使用它的 compareTo(或 compare)方法对所有元素进行比较,因此从 set 的观点来看,此方法认为相等的两个元素就是相等的。
------解决方案--------------------
TreeSet 默认用的 TreeMap 作为 delegate
   public TreeSet() {
	this(new TreeMap<E,Object>());
   }
public V put(K key, V value) 是TreeMap的。
------解决方案--------------------