LinkedHashSet 源码分析

package java.util;

/**
 * 1)由 LinkedHashMap 支持的 Set 接口实现,元素按照插入顺序进行迭代,
 * 迭代顺序不会因为重新插入同一元素受到影响,允许使用 null 元素。
 */
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.
     * 创建一个空的 LinkedHashSet,底层的 LinkedHashMap 具有指定的初始容量 initialCapacity
     * 和加载因子  loadFactor。
     */
    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).
     * 创建一个空的 LinkedHashSet,底层的 LinkedHashMap 具有指定的初始容量 initialCapacity
     * 和加载因子  0.75。
     */
    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).
     * 创建一个空的 LinkedHashSet,底层的 LinkedHashMap 具有指定的初始容量 16
     * 和加载因子  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).
     * 创建一个空的 LinkedHashSet,底层的 LinkedHashMap 具有指定的初始容量 Math.max(2*c.size(), 11)
     * 和加载因子  0.75
     */
    public LinkedHashSet(Collection<? extends E> c) {
        super(Math.max(2*c.size(), 11), .75f, true);
        addAll(c);
    }

    /**
     * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
     * and <em>fail-fast</em> {@code Spliterator} over the elements in this set.
     * 基于 LinkedHashSet 中的所有元素创建一个分割迭代器,主要用于并行处理。
     * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
     * {@link Spliterator#DISTINCT}, and {@code ORDERED}.  Implementations
     * should document the reporting of additional characteristic values.
     * 迭代器的元素个数是确定的 {@link Spliterator#SIZED},元素是保证唯一的 {@link Spliterator#DISTINCT}
     * 和有序的 {@code SpliteratorORDERED}
     *
     * @since 1.8
     */
    @Override
    public Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, Spliterator.DISTINCT | Spliterator.ORDERED);
    }
}

猜你喜欢

转载自www.cnblogs.com/zhuxudong/p/9350707.html