public boolean add(E e) { return m.put(e, PRESENT)==null; }
public V put(K key, V value) { Entry<K,V> t = root;//根元素 if (t == null) { // TBD: // 5045147: (coll) Adding null to an empty TreeSet should // throw NullPointerException // // compare(key, key); // type check root = new Entry<K,V>(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); //比较两个元素的key值,也就是Set中元素的值 if (cmp < 0) //如果小于0则放到当前元素的左边 t = t.left; else if (cmp > 0) //如果大于0则放到当前元素的右边 t = t.right; else return t.setValue(value); //否则,即返回0时调用java.util.TreeMap.Entry.setValue(value)方法 } while (t != null); } //后面代码省略...... }下面是java.util.TreeMap.Entry.setValue(value)方法源码:
public V setValue(V value) { V oldValue = this.value; this.value = value; return oldValue; }
原文地址:http://blog.csdn.net/xtayfjpk/article/details/24806887