天天看點

e.HashSet與LinkedHashSet源碼解析(1.7)

java容器類分為兩大類,一部分是Map,用來存放鍵值對。另一部分是Collection,用來存放獨立的元素。Collection分為List與Set,List可以有重複順序,Set則不行

我們要分析的HashSet與LinkedHashSet底層都是用的HashMap與LinkedHashMap的key來實作的,這裡将具體分析下具體的封裝

<pre name="code" class="java">public class HashSet<E>
    extends AbstractSet<E>
    implements Set<E>, Cloneable, java.io.Serializable
           

屬性: 

<span style="white-space:pre">	</span>private transient HashMap<E,Object> map;//沒錯,就是用它的key來存儲元素的

    <span style="white-space:pre">	</span>private static final Object PRESENT = new Object();//常量,它的作用基本可以看出來,是用來當做某種初始值,如果往下看的話應該明白,就是作為Map的Value
           

構造方法:

//看到map=new HashMap(),往下就是一切盡在不言中了,我們隻要看看set與map的聯系即可
           
public HashSet() {
        map = new HashMap<>();
    }

    
    public HashSet(Collection<? extends E> c) {
        map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
        addAll(c);
    }

   
    public HashSet(int initialCapacity, float loadFactor) {
        map = new HashMap<>(initialCapacity, loadFactor);
    }


    public HashSet(int initialCapacity) {
        map = new HashMap<>(initialCapacity);
    }

    //注意,這個将會在LinkedHashSet裡頭用到
    HashSet(int initialCapacity, float loadFactor, boolean dummy) {
        map = new LinkedHashMap<>(initialCapacity, loadFactor);
    }
           

方法:

疊代器:這裡赤裸裸的直接用上了keySet.iterator();

public Iterator<E> iterator() {
        return map.keySet().iterator();
    }
           

size與isEmpty一樣,也是直接調用了map對象的方法

public int size() {
        return map.size();
    }

    public boolean isEmpty() {
        return map.isEmpty();
    }
           

往下一個套路,就不再多說了

***********************************************************************************

public class LinkedHashSet<E>
    extends HashSet<E>
    implements Set<E>, Cloneable, java.io.Serializable 
           

類聲明與HashSet大同小異,LinkedHashSet是HashSet的子類

他的構造方法通過調用HashSet的第五個構造方法,建立一個lingkedHashHap,其餘與HashSet相同

public class LinkedHashSet<E>
    extends HashSet<E>
    implements Set<E>, Cloneable, java.io.Serializable {

    private static final long serialVersionUID = -2851667679971038690L;

    /**
     * Constructs a new, empty linked hash set with the specified initial
     * capacity and load factor.
     *
     * @param      initialCapacity the initial capacity of the linked hash set
     * @param      loadFactor      the load factor of the linked hash set
     * @throws     IllegalArgumentException  if the initial capacity is less
     *               than zero, or if the load factor is nonpositive
     */
    public LinkedHashSet(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor, true);
    }

    /**
     * Constructs a new, empty linked hash set with the specified initial
     * capacity and the default load factor (0.75).
     *
     * @param   initialCapacity   the initial capacity of the LinkedHashSet
     * @throws  IllegalArgumentException if the initial capacity is less
     *              than zero
     */
    public LinkedHashSet(int initialCapacity) {
        super(initialCapacity, .75f, true);
    }

    /**
     * Constructs a new, empty linked hash set with the default initial
     * capacity (16) and load factor (0.75).
     */
    public LinkedHashSet() {
        super(16, .75f, true);
    }

    /**
     * Constructs a new linked hash set with the same elements as the
     * specified collection.  The linked hash set is created with an initial
     * capacity sufficient to hold the elements in the specified collection
     * and the default load factor (0.75).
     *
     * @param c  the collection whose elements are to be placed into
     *           this set
     * @throws NullPointerException if the specified collection is null
     */
    public LinkedHashSet(Collection<? extends E> c) {
        super(Math.max(2*c.size(), 11), .75f, true);
        addAll(c);
    }
}