Java Coder

          AbstractSet 抽象類分析

          java.util.AbstractSet抽象類繼承自 AbstractCollection 抽象類,所有Set的實現類都直接或者間接繼承AbstractSet父類。
          AbstractSet類包括equals(),hashCode(),removeAll(c)方法,這些方法都被實現了,子類無需再重寫。
          AbstractSet類提供了實現Set集合的基本框架,方便HashSet、TreeSet這些子類的實現。

            1 package java.util;
            2 
            3 public abstract class AbstractSet<E> extends AbstractCollection<E> implements
            4         Set<E> {
            5     /**
            6      * Sole constructor. (For invocation by subclass constructors, typically
            7      * implicit.)
            8      */
            9     protected AbstractSet() {
           10     }
           11 
           12     // Comparison and hashing
           13 
           14     /**
           15      * Compares the specified object with this set for equality. Returns
           16      * <tt>true</tt> if the given object is also a set, the two sets have the
           17      * same size, and every member of the given set is contained in this set.
           18      * This ensures that the <tt>equals</tt> method works properly across
           19      * different implementations of the <tt>Set</tt> interface.
           20      * <p>
           21      * 
           22      * This implementation first checks if the specified object is this set; if
           23      * so it returns <tt>true</tt>. Then, it checks if the specified object is a
           24      * set whose size is identical to the size of this set; if not, it returns
           25      * false. If so, it returns <tt>containsAll((Collection) o)</tt>.
           26      * 
           27      * @param o
           28      *            object to be compared for equality with this set
           29      * @return <tt>true</tt> if the specified object is equal to this set
           30      */
           31     public boolean equals(Object o) {
           32         // 是否指向同一個對象?
           33         if (o == this)
           34             return true;
           35         
           36         // 是否都是Set類?
           37         if (!(o instanceof Set))
           38             return false;
           39         
           40         Collection c = (Collection) o;
           41         // 先比較大小
           42         if (c.size() != size())
           43             return false;
           44         try {
           45         // 再比較所有的元素
           46             return containsAll(c);
           47         // 處理containsAll(c)拋出的異常
           48         } catch (ClassCastException unused) {
           49             return false;
           50         } catch (NullPointerException unused) {
           51             return false;
           52         }
           53     }
           54 
           55     /**
           56      * Returns the hash code value for this set. The hash code of a set is
           57      * defined to be the sum of the hash codes of the elements in the set, where
           58      * the hash code of a <tt>null</tt> element is defined to be zero. This
           59      * ensures that <tt>s1.equals(s2)</tt> implies that
           60      * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt> and
           61      * <tt>s2</tt>, as required by the general contract of
           62      * {@link Object#hashCode}.
           63      * 
           64      * <p>
           65      * This implementation iterates over the set, calling the <tt>hashCode</tt>
           66      * method on each element in the set, and adding up the results.
           67      * 
           68      * @return the hash code value for this set
           69      * @see Object#equals(Object)
           70      * @see Set#equals(Object)
           71      */
           72     public int hashCode() {
           73         int h = 0;
           74         // 使用迭代器遍歷所有的元素
           75         Iterator<E> i = iterator();
           76         while (i.hasNext()) {
           77             E obj = i.next();
           78             if (obj != null)
           79                 // 加上每個元素的hashCode
           80                 h += obj.hashCode();
           81         }
           82         return h;
           83     }
           84 
           85     /**
           86      * Removes from this set all of its elements that are contained in the
           87      * specified collection (optional operation). If the specified collection is
           88      * also a set, this operation effectively modifies this set so that its
           89      * value is the <i>asymmetric set difference</i> of the two sets.
           90      * 
           91      * <p>
           92      * This implementation determines which is the smaller of this set and the
           93      * specified collection, by invoking the <tt>size</tt> method on each. If
           94      * this set has fewer elements, then the implementation iterates over this
           95      * set, checking each element returned by the iterator in turn to see if it
           96      * is contained in the specified collection. If it is so contained, it is
           97      * removed from this set with the iterator's <tt>remove</tt> method. If the
           98      * specified collection has fewer elements, then the implementation iterates
           99      * over the specified collection, removing from this set each element
          100      * returned by the iterator, using this set's <tt>remove</tt> method.
          101      * 
          102      * <p>
          103      * Note that this implementation will throw an
          104      * <tt>UnsupportedOperationException</tt> if the iterator returned by the
          105      * <tt>iterator</tt> method does not implement the <tt>remove</tt> method.
          106      * 
          107      * @param c
          108      *            collection containing elements to be removed from this set
          109      * @return <tt>true</tt> if this set changed as a result of the call
          110      * @throws UnsupportedOperationException
          111      *             if the <tt>removeAll</tt> operation is not supported by this
          112      *             set
          113      * @throws ClassCastException
          114      *             if the class of an element of this set is incompatible with
          115      *             the specified collection (optional)
          116      * @throws NullPointerException
          117      *             if this set contains a null element and the specified
          118      *             collection does not permit null elements (optional), or if
          119      *             the specified collection is null
          120      * @see #remove(Object)
          121      * @see #contains(Object)
          122      */
          123     public boolean removeAll(Collection<?> c) {
          124         boolean modified = false;
          125 
          126         // 對元素少的集合使用迭代器
          127         if (size() > c.size()) {
          128             for (Iterator<?> i = c.iterator(); i.hasNext();)
          129                 // Set集合刪除元素,記錄操作是否成功
          130                 modified |= remove(i.next());
          131         } else {
          132             for (Iterator<?> i = iterator(); i.hasNext();) {
          133                 if (c.contains(i.next())) {
          134                 // 迭代器刪除元素,記錄成功
          135                     i.remove();
          136                     modified = true;
          137                 }
          138             }
          139         }
          140         return modified;
          141     }
          142 
          143 }
          144 


          posted on 2008-07-21 22:20 fred.li 閱讀(263) 評論(0)  編輯  收藏 所屬分類: java.util 包分析

          主站蜘蛛池模板: 阳谷县| 阿克苏市| 萍乡市| 岱山县| 旬邑县| 呼伦贝尔市| 伊宁市| 砀山县| 景东| 新安县| 安平县| 瓮安县| 和平县| 建平县| 高州市| 嘉峪关市| 额尔古纳市| 昌邑市| 泰宁县| 讷河市| 措美县| 刚察县| 合阳县| 迭部县| 莱阳市| 浦江县| 屏南县| 文水县| 通道| 壤塘县| 吴江市| 西宁市| 满洲里市| 平山县| 南丹县| 通州市| 恩施市| 上虞市| 韩城市| 盐山县| 荆州市|