首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

ConcurrentSkipListSet的add(E e)步骤注释误人子弟

2012-12-24 
ConcurrentSkipListSet的add(E e)方法注释误人子弟!一个项目用到ConcurrentSkipListSet.add(E e)方法,过程

ConcurrentSkipListSet的add(E e)方法注释误人子弟!

一个项目用到ConcurrentSkipListSet.add(E e)方法,过程中总觉得元素添加数量有问题,故调试之。


查看add()方法的javadoc,其注释为:
如果此 set 中不包含指定元素,则添加指定元素。更确切地讲,如果此 set 不包含满足 e.equals(e2) 的元素
e2,则向 set 中添加指定的元素 e。如果此 set 已经包含该元素,则调用不更改该 set 并返回
false。



根据注释,只要元素的equals()方法判断不相等就能加入到Set中,可调试发现不是这么回事:

?

public boolean add(E e) {    return m.putIfAbsent(e, Boolean.TRUE) == null;}

?
m为 ConcurrentSkipListMap,它的putIfAbsent()方法实现是:

    public V putIfAbsent(K key, V value) {        if (value == null)            throw new NullPointerException();        return doPut(key, value, true);    }    private V doPut(K kkey, V value, boolean onlyIfAbsent) {        Comparable super K> key = comparable(kkey);        for (;;) {            Node b = findPredecessor(key);            Node n = b.next;            for (;;) {                if (n != null) {                    Node f = n.next;                    if (n != b.next)               // inconsistent read                        break;;                    Object v = n.value;                    if (v == null) {               // n is deleted                        n.helpDelete(b, f);                        break;                    }                    if (v == n || b.value == null) // b is deleted                        break;                    int c = key.compareTo(n.key);                    if (c > 0) {                        b = n;                        n = f;                        continue;                    }                    if (c == 0) {                        if (onlyIfAbsent || n.casValue(v, value))                            return (V)v;                        else                            break; // restart if lost race to replace value                    }                    // else c  z = new Node(kkey, value, n);                if (!b.casNext(n, z))                    break;         // restart if lost race to append to b                int level = randomLevel();                if (level > 0)                    insertIndex(z, level);                return null;            }        }    }
?实际上Set中的添加是根据元素的compareTo()方法在比较!如果compareTo()返回0,就认为2个元素相等,跟equals()方法根本没有关系!JAVA SDK 的JavaDoc也不靠谱啊~~

?

?

1 楼 tsyj810883979 2011-10-21   /**
     * The underlying map. Uses Boolean.TRUE as value for each
     * element.  This field is declared final for the sake of thread
     * safety, which entails some ugliness in clone()
     */
    private final ConcurrentNavigableMap<E,Object> m;

    /**
     * Constructs a new, empty set that orders its elements according to
     * their {@linkplain Comparable natural ordering}.
     */
    public ConcurrentSkipListSet() {
        m = new ConcurrentSkipListMap<E,Object>();
    }

    /**
     * Constructs a new, empty set that orders its elements according to
     * the specified comparator.
     *
     * @param comparator the comparator that will be used to order this set.
     *        If <tt>null</tt>, the {@linkplain Comparable natural
     *        ordering} of the elements will be used.
     */
    public ConcurrentSkipListSet(Comparator<? super E> comparator) {
        m = new ConcurrentSkipListMap<E,Object>(comparator);
    }
JDK源码中的注释

热点排行