JDK 1.8 AbstractCollection源码解析

类信息

该类实现自Collection

public abstract class AbstractCollection<E> implements Collection<E> 

属性

 

方法

contains(Object o)

  public boolean contains(Object o) {
        //获得该对象的迭代
        Iterator<E> it = iterator();
        //判断o是否为空防止 o.equals异常
        if (o==null) {
            //循环比对 null用 == 不发生异常
            while (it.hasNext())
                if (it.next()==null)
                    return true;
        } else {
            //循环遍历,不为空就调用 equals方法
            while (it.hasNext())
                if (o.equals(it.next()))
                    return true;
        }
        //找不到reutnr false
        return false;
}

toArray()

  public Object[] toArray() {
        //根据该集合所存储的元素长度绝地 r 数组的长度
        Object[] r = new Object[size()];
        //获得该集合对象迭代
        Iterator<E> it = iterator();
        //for 循环遍历数组 填充对于值
        for (int i = 0; i < r.length; i++) {
            //元素少于预期
            if (! it.hasNext())
                //截断:一共有多少个元素就有多少数组空间。
                return Arrays.copyOf(r, i);
            //从迭代中获取对应的没一位填充进 r 数组中
            r[i] = it.next();
        }
        //三元运输判断 如果当前集合还有元素为装入数组那么就调用
//        finishToArray 该情况可能在多线程环境下发生 
//        如果一切没问题那么返回添加好元素的 r 数组
        return it.hasNext() ? finishToArray(r, it) : r;
}

 

toArray(T[] a)

public <T> T[] toArray(T[] a) {
        //预估集合大小
        int size = size();
        //如果 a的长度大于等于当前预估长度 返回 a 否则
        //调用Array.newInstance 创建具有指定组件类型和长度的新数组。
        // 调用此方法相当于创建一个数组
        T[] r = a.length >= size ? a :
                (T[])java.lang.reflect.Array
                        .newInstance(a.getClass().getComponentType(), size);
        //获得当前集合迭代对象
        Iterator<E> it = iterator();

        //遍历 r 数组
        for (int i = 0; i < r.length; i++) {
              // 元素少于预期
            if (! it.hasNext()) {
                //如果 a 和 r 是一个对象
                if (a == r) {
                    //把 r 数字的第 i 位设为 null
                    r[i] = null;
                    
                } else if (a.length < i) {
                    //返回长度为 i 的数组,并将 r 中的每个元素都
                    //按顺序拷贝进新数组 并 返回
                    return Arrays.copyOf(r, i);
                } else {
                    //a 可以容纳全部元素那么久将r数组中的元素拷贝进 a 数组
                    System.arraycopy(r, 0, a, 0, i);
                   //如果 a的长度大于 i  a 的第i位就设为null
                    if (a.length > i) {
                        a[i] = null;
                    }
                }
                return a;
            }
            //对于元素复制给数组 r
            r[i] = (T)it.next();
        }
        // 如果比预期更多的元素 调用 finishToArray
        return it.hasNext() ? finishToArray(r, it) : r;
}

 

finishToArray(T[] r, Iterator<?> it)

 /**
     * 当迭代器*返回的元素多于预期时,重新分配在toArray
     * 中使用的数组,并从*迭代器完成填充。
     * @param r 数组,充满先前存储的元素
     * @param it 此集合中正在进行的迭代器
     * @return 包含给定数组中元素的数组,以及迭代器返回的
     * 任何其他元素,修剪为size
     */
    private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
        //r 数组长度
        int i = r.length;
        //继续迭代
        while (it.hasNext()) {
            //获得当前 r 数组长度
            int cap = r.length;
            //判断 i 是否和 cap 相等
            if (i == cap) {
                //翻译: cap + (cap / 2) + 1 注:使用位移是为了更好的效率
                int newCap = cap + (cap >> 1) + 1;
                //如果新的数组长度大于  MAX_ARRAY_SIZE
                if (newCap - MAX_ARRAY_SIZE > 0)
                    //newCap的值将调用  hugeCapacity(cap + 1)求得
                    newCap = hugeCapacity(cap + 1);
                //将r数组扩容到newCap长度
                r = Arrays.copyOf(r, newCap);
            }
            //把集合里的元素一一对应存入 r 数组
            r[i++] = (T)it.next();
        }
        //Arrays.copyOf(r,i)的目的是为了将数组的容量和数组所存储的元素
        //个数相匹配
        return (i == r.length) ? r : Arrays.copyOf(r, i);
}

hugeCapacity(int minCapacity)

 

  private static int hugeCapacity(int minCapacity) {
        //minCapacity小于0抛异常
        if (minCapacity < 0)
            throw new OutOfMemoryError
                    ("Required array size too large");
        //minCapacity 是否大于MAX_ARRAY_SIZE
        //大于返回  Integer.MAX_VALUE
        //小于返回  MAX_ARRAY_SIZE
       return (minCapacity > MAX_ARRAY_SIZE) ?
               Integer.MAX_VALUE :
                MAX_ARRAY_SIZE;
}

 

 add(E e)

//始终抛异常
public boolean add(E e) {
        throw new UnsupportedOperationException();
}

remove(Object o)

  public boolean remove(Object o) {
        //当前对象迭代
        Iterator<E> it = iterator();
        //防止抛空指针异常
        if (o==null) {
            while (it.hasNext()) {
                if (it.next()==null) {
                    //调用迭代器的remove方法删除元素
                    it.remove();
                    return true;
                }
            }
        } else {
            while (it.hasNext()) {
                if (o.equals(it.next())) {
                    it.remove();
                    return true;
                }
            }
        }
        //没找到要删除的元素返回 false
        return false;
 }

 

containsAll(Collection<?> c)

 

 public boolean containsAll(Collection<?> c) {
        //遍历c集合
        for (Object e : c)
            //如果 contains方法返回为false那么该方法返回为false
            if (!contains(e))
                return false;
        //本集合包含c集合的所以元素 返回 true    
        return true;
}

addAll(Collection<? extends E> c)

 public boolean addAll(Collection<? extends E> c) {
        //modified 初始为 false
        boolean modified = false;
        for (E e : c)
            //只有有一个元素成功添加 即 add(e)返回 true
            //那么 modified 就为true
            if (add(e))
                modified = true;
        return modified;
}

removeAll(Collection<?> c)

public boolean removeAll(Collection<?> c) {
        boolean modified = false;
        //it 当前集合迭代
        Iterator<?> it = iterator();
        while (it.hasNext()) {
            //c集合是否包含该元素
            if (c.contains(it.next())) {
                //如果包含就删除
                it.remove();
                //modified 返回 true
                modified = true;
            }
        }
        return modified;
    }

 

retainAll(Collection<?> c)

    public boolean retainAll(Collection<?> c) {
        boolean modified = false;
        //it 该对象迭代
        Iterator<E> it = iterator();
        //迭代遍历所有元素
        while (it.hasNext()) {
            //c不包含该元素指向if体
            if (!c.contains(it.next())) {
                //删除该元素
                it.remove();
                modified = true;
            }
        }
        return modified;
    }

 

clear()

//清除该集合所以元素的方法
    public void clear() {
        //当前集合迭代对象
        Iterator<E> it = iterator();
        //循环迭代删除每一个元素
        while (it.hasNext()) {
            it.next();
            it.remove();
        }
    }

toString() 

 public String toString() {
        //当前集合对象迭代对象
        Iterator<E> it = iterator();
        //如果是一个空集合 返回 "[]"
        if (! it.hasNext())
            return "[]";

        //定义一个StringBuilder (PS:因为StringBuilder可变字符串String不可变字符串)
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        //死循环
        for (;;) {
            //e迭代的每个元素
            E e = it.next();
            //如果e是该集合对象本身添加"(this Collection)"
            //否则添加 e
            sb.append(e == this ? "(this Collection)" : e);
            //如果添加完了所有元素执行该 if条件
            if (! it.hasNext())
                //sb转换为String并返回
                return sb.append(']').toString();
            sb.append(',').append(' ');
        }
 }

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值