【Java X 源码剖析】Collection的源码分析-JDK1.8-仍在更新

Collection接口下的结构

 

目录

Set

HashSet

LinkedHashSet(父类HashSet,底层Map为LinkedHashMap)

TreeSet(依赖TreeMap)

ConcurrentSkipListSet

 

List-Queue

PriorityQueue(默认小顶堆)

①public boolean offer(E e) / public boolean add(E e)

List

LinkedList

addAll(int index, Collection c):

①public boolean add(E e) 

②public boolean remove(Object o)

ArrayList:动态扩容

①public boolean add(E e)

②public E set(int index, E element) 

③public int indexOf(Object o) :可以查找null元素,意味ArrayList可以存放null

④public E get(int index)

⑤public E remove(int index):删除时会移动大量元素

Vector(过时的类,每个方法都有Synchronized)[可以存null]

Vector属性

构造函数

 

Stack

 

 


Set

HashSet

一些属性

*所有的键都有同一个值PRESENT

public class HashSet<E>
    extends AbstractSet<E>
    implements Set<E>, Cloneable, java.io.Serializable
{
    private transient HashMap<E,Object> map;

    // Dummy value to associate with an Object in the backing Map
    //用作所有键的值,因为HashSet中只存键不存值
    private static final Object PRESENT = new Object();
}

方法都是调用HashMap中的方法,不再重复


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

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


    public boolean contains(Object o) {
        return map.containsKey(o);
    }


    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

    public boolean remove(Object o) {
        return map.remove(o)==PRESENT;
    }

    public void clear() {
        map.clear();
    }

 

 

LinkedHashSet(父类HashSet,底层Map为LinkedHashMap)

一些属性

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

    //没有属性,都是调用父类的属性

}

构造函数

调用父类的构造函数,使其底层实现变成LinkedHashMap

//两个方法调用同一个父类的构造方法
public LinkedHashSet(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor, true);
    }

public LinkedHashSet(int initialCapacity) {
        super(initialCapacity, .75f, true);
    }

public LinkedHashSet() {
        super(16, .75f, true);
    }

public LinkedHashSet(Collection<? extends E> c) {
        super(Math.max(2*c.size(), 11), .75f, true);
        addAll(c);
    }
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
        map = new LinkedHashMap<>(initialCapacity, loadFactor);
    }

 

 

TreeSet(依赖TreeMap)

一些属性

public class TreeSet<E> extends AbstractSet<E>
    implements NavigableSet<E>, Cloneable, java.io.Serializable
{
    /**
     * The backing map.
     */
    private transient NavigableMap<E,Object> m;

    // Dummy value to associate with an Object in the backing Map
    //同样的套路:所有键的value都是PRESENT
    private static final Object PRESENT = new Object();
}

 

构造函数

    public TreeSet(Comparator<? super E> comparator) {
        this(new TreeMap<>(comparator));
    }

    public TreeSet() {
        this(new TreeMap<E,Object>());
    }

    //还有别的构造方法就不一一列举了

 

*底层方法实现同TreeMap

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

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

    public boolean contains(Object o) {
        return m.containsKey(o);
    }


    public boolean add(E e) {
        return m.put(e, PRESENT)==null;
    }


    public boolean remove(Object o) {
        return m.remove(o)==PRESENT;
    }


    public void clear() {
        m.clear();
    }

 

 

 

ConcurrentSkipListSet

 

一些属性

 

构造函数

 

 

 

 

List-Queue

PriorityQueue(默认小顶堆)

①public boolean offer(E e) / public boolean add(E e)

    public boolean add(E e) {
        return offer(e);
    }

    public boolean offer(E e) {
        if (e == null)
            throw new NullPointerException();
        modCount++;
        int i = size;
        if (i >= queue.length)
            grow(i + 1);
        size = i + 1;
        if (i == 0)
            queue[0] = e;
        else
            siftUp(i, e);
        return true;
    }

    /**
     * Increases the capacity of the array.
     *
     * @param minCapacity the desired minimum capacity
     */
    //数组容量已经不满足下标了,遂扩容
    private void grow(int minCapacity) {
        int oldCapacity = queue.length;

        // Double size if small; else grow by 50%
        //如果原本容量小于64就变2n+2;否则变1.5n
        int newCapacity = oldCapacity + ((oldCapacity < 64) ?
                                         (oldCapacity + 2) :
                                         (oldCapacity >> 1));
        // overflow-conscious code
        //数组最大值 or Integer_MAX_VALUE
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        queue = Arrays.copyOf(queue, newCapacity);
    }

    private void siftUp(int k, E x) {
        if (comparator != null)
            siftUpUsingComparator(k, x);
        else
            siftUpComparable(k, x);
    }

    //有比较器(默认小顶堆) k为x在数组中的下标
    @SuppressWarnings("unchecked")
    private void siftUpUsingComparator(int k, E x) {
        while (k > 0) {
            //父亲结点数组下标
            int parent = (k - 1) >>> 1;
            Object e = queue[parent];
            //比父亲大,小顶堆,就不用在往上移了
            if (comparator.compare(x, (E) e) >= 0)
                break;
            //暂时不用赋值x,最后循环出去再赋值
            queue[k] = e;
            k = parent;
        }
        queue[k] = x;
    }

    //Queue没有比较器,使用插入的对象的比较器
    @SuppressWarnings(
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值