e.HashSet与LinkedHashSet源码解析(1.7)

时间:2021-11-21 17:55:15

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);
}
}