AbstractCollection源码学习

描述

This class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface.
该类提供了Collection接口的骨架实现,以最小化实现此接口所需的工作量。

从这段描述可以看出来,这个抽象类提供了Connection中方法的大部分默认实现;

默认实现方法

contains()

public boolean contains(Object o) {
        //寻找第一个满足情况的值,允许空值
        Iterator<E> it = iterator();
        if (o==null) {
            while (it.hasNext())
                if (it.next()==null)
                    return true;
        } else {
            while (it.hasNext())
                if (o.equals(it.next()))
                    return true;
        }
        return false;
    }

toArray()

public Object[] toArray() {
        // Estimate size of array; be prepared to see more or fewer elements
        // 在多线程中,可能存在集合结构上的修改(增、删)
        Object[] r = new Object[size()];
        Iterator<E> it = iterator();
        for (int i = 0; i < r.length; i++) {
            if (! it.hasNext()) // fewer elements than expected  元素少于预期
                return Arrays.copyOf(r, i);
            r[i] = it.next();
        }
        //集合元素是否全部数组化
        return it.hasNext() ? finishToArray(r, it) : r;
    }

/*
	返回指定类型的数组
*/
public <T> T[] toArray(T[] a) {
        // Estimate size of array; be prepared to see more or fewer elements
        int size = size();     //原数组长度

        //选择数组 —— 形参数组 / 新建形参类型数组(类型转换)
        T[] r = a.length >= size ? a :
                  (T[])java.lang.reflect.Array
                  .newInstance(a.getClass().getComponentType(), size);
        Iterator<E> it = iterator();

        for (int i = 0; i < r.length; i++) {
            if (! it.hasNext()) { // fewer elements than expected
                if (a == r) {
                    r[i] = null; // null-terminate
                } else if (a.length < i) {
                    return Arrays.copyOf(r, i);
                } else {
                    System.arraycopy(r, 0, a, 0, i);
                    if (a.length > i) {
                        a[i] = null;
                    }
                }
                return a;
            }

            //强制类型转换 —— 有可能出现类型转换错误
            r[i] = (T)it.next();
        }
        // more elements than expected
        // 元素数量超出预期
        return it.hasNext() ? finishToArray(r, it) : r;
    }


private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
        int i = r.length;     //原数组容量
        //是否存在下一个元素
        while (it.hasNext()) {
            //当前数组容量
            int cap = r.length;
            //无剩余空间,进行扩容
            if (i == cap) {
                //分配新长度 —— 原长度 + 原长度一半 + 1
                int newCap = cap + (cap >> 1) + 1;
                // overflow-conscious code
                //最大容量校验
                if (newCap - MAX_ARRAY_SIZE > 0)
                    //容量加1 --> 获取最大容量(数组最大容量 / 整数最大值)
                    newCap = hugeCapacity(cap + 1);
                //元素拷贝
                r = Arrays.copyOf(r, newCap);
            }
            r[i++] = (T)it.next();
        }
        // trim if overallocated 
        // 过度分配时修剪
        return (i == r.length) ? r : Arrays.copyOf(r, i);
    }

	//极限容量的分配 —— MAX_ARRAY_SIZE / Integer.MAX_VALUE 
    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError
                ("Required array size too large");
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

其余方法实现逻辑较为简单

小结

  • AbstractCollection提供了大致的实现,除必须实现的方法之外,我们可以自定义自己的实现类
  • 对于整体结构来说,AbstractConnection并没有定义修改元素的方法
  • 在数组化的过程中,其并没有限制元素在读过程中写入,因此可能会出现数组中元素与集合中元素个数不一致的情况(具体看添加元素与迭代器的位置关系)
  • AbstractCollection 是支持空值的
  • 在toArray(T[] a)中需要注意类型的转化问题,可能会出现数组转储类型异常

写在最后的话

以上是个人的学习笔记。如果有什么错误或偏差,请告知,让我们一起学习、进步!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值