java集合框架源码分析

Java Collection FrameWork 源码分析(JDK8)

1.ArrayList

 

添加元素的方式和移除元素

add(E e)

add(int index,E element)

addAll(Collection<? extends E> c)

addAll(int index, Collection<? extends E> c)

remove(int index)

remove(Object o)

 ​
 public boolean add(E e) {
     //确保添加元素后的容器(数组.length)是否放得下,size为数组中元素个数,size+1是添加后元素个数
     ensureCapacityInternal(size + 1);  // Increments modCount!!
     elementData[size++] = e;
     return true;
 }
 private static final int DEFAULT_CAPACITY = 10;
 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
 private void ensureCapacityInternal(int minCapacity) {
     ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
 }
 private static int calculateCapacity(Object[] elementData, int minCapacity) {
     //如果数组elementData为空,说明现在数组还没被初始化,仅仅是有个引用
     //那么minCapacity=size+1=1,空的数组没有办法存放元素,那么就将默认数组容量设为10并返回
     //如果数组elementData不为空直接返回minCapacity
     if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
         return Math.max(DEFAULT_CAPACITY, minCapacity);
     }
     return minCapacity;
 }
 private void ensureExplicitCapacity(int minCapacity) {
     //List,map这些集合结构中常能看到modCount属性,在对集合进行新增或移除操作时会使modCount+1,这     //是一个记录集合变化次数的变量。作用是在使用迭代器Iterator对集合进行遍历时,用modCount来判断集     //合内数据没有发生变化,否则会抛出异常。
     链接:https://www.jianshu.com/p/d01e9cf207f9
 ​
 ​
     //判断是否需要扩容
     // overflow-conscious code
     if (minCapacity - elementData.length > 0)
         grow(minCapacity);
 }
 private void grow(int minCapacity) {
     // overflow-conscious code
     //将当前数组长度赋值为老数组容量
     int oldCapacity = elementData.length;
    //将老数组容量扩容1.5倍后赋值给新数组容量
     int newCapacity = oldCapacity + (oldCapacity >> 1);
     //这种情况出现的原因为如果返回的minCapacity==10,此时elementDate还为{},没有被初始化,
     //newCapacity==oldCapacity==0<minCapacity==10
     if (newCapacity - minCapacity < 0)
         newCapacity = minCapacity;
     //在这里如果扩容后的newCapacity移除为负数也符合条件,负数-正数=正数>0,int范围为-2^31~2^31-1
     if (newCapacity - MAX_ARRAY_SIZE > 0)
         newCapacity = hugeCapacity(minCapacity);
     // minCapacity is usually close to size, so this is a win:
     elementData = Arrays.copyOf(elementData, newCapacity);
 }
 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
 private static int hugeCapacity(int minCapacity) {
     if (minCapacity < 0) // overflow
         throw new OutOfMemoryError();
     return (minCapacity > MAX_ARRAY_SIZE) ?
         Integer.MAX_VALUE :
         MAX_ARRAY_SIZE;
 }
@SuppressWarnings("unchecked")
public static <T> T[] copyOf(T[] original, int newLength) {
    return (T[]) copyOf(original, newLength, original.getClass());
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    @SuppressWarnings("unchecked")
    T[] copy = ((Object)newType == (Object)Object[].class)
        ? (T[]) new Object[newLength]
        : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;
}
//调用了本地方法arraycopy
/**
  * @param      src      the source array.
  * @param      srcPos   starting position in the source array.
  * @param      dest     the destination array.
  * @param      destPos  starting position in the destination data.
  * @param      length   the number of array elements to be copied.
  *\
  把src数组从srcPos位置其长度为length复制到dest数组的destPos位置后
public static native void arraycopy(Object src,  int  srcPos,
                                    Object dest, int destPos,
                                    int length);

 

int[] arr = new int[100];
int[] arr2 = {11,12,23,23};
for (int i = 0; i <10 ; i++) {
    arr[i]=i;
}
System.arraycopy(arr, 0, arr, 4, 10);
System.out.println(Arrays.toString(arr));
System.arraycopy(arr2, 0, arr, 0, 4);
System.out.println(Arrays.toString(arr));

[0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [11, 12, 23, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

add(int index,E element)

public void add(int index, E element) {
    //判断添加位置合法性
    rangeCheckForAdd(index);
	
    //上面分析过了
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    System.arraycopy(elementData, index, elementData, index + 1,
                     size - index);
    elementData[index] = element;
    size++;
}
 private void rangeCheckForAdd(int index) {
     if (index > size || index < 0)
         throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 }

addAll(Collection<? extends E> c)

 public boolean addAll(Collection<? extends E> c) {
     Object[] a = c.toArray();
     int numNew = a.length;
     ensureCapacityInternal(size + numNew);  // Increments modCount
     System.arraycopy(a, 0, elementData, size, numNew);
     size += numNew;
     return numNew != 0;
 }

addAll(int index, Collection<? extends E> c)

 public boolean addAll(int index, Collection<? extends E> c) {
     rangeCheckForAdd(index);
 ​
     Object[] a = c.toArray();
     int numNew = a.length;
     ensureCapacityInternal(size + numNew);   // Increments modCount
 ​
     int numMoved = size - index;
     if (numMoved > 0)
         System.arraycopy(elementData, index, elementData, index + numNew,
                          numMoved);
 ​
     System.arraycopy(a, 0, elementData, index, numNew);
     size += numNew;
     return numNew != 0;
 }

remove(int index)

 public E remove(int index) {
     rangeCheck(index);
 ​
     modCount++;
     E oldValue = elementData(index);
 ​
     int numMoved = size - index - 1;
     //表示移动元素的个数,也是元素移动的步数
     if (numMoved > 0)
         System.arraycopy(elementData, index+1, elementData, index,
                          numMoved);
     elementData[--size] = null; // clear to let GC do its work
 ​
     return oldValue;
 }

remove(Object o)

   public boolean remove(Object o) {
         if (o == null) {
             for (int index = 0; index < size; index++)
                 if (elementData[index] == null) {
                     fastRemove(index);
                     return true;
                 }
         } else {
             for (int index = 0; index < size; index++)
                 if (o.equals(elementData[index])) {
                     fastRemove(index);
                     return true;
                 }
         }
         return false;
     }
 private void fastRemove(int index) {
     modCount++;
     int numMoved = size - index - 1;
     if (numMoved > 0)
         System.arraycopy(elementData, index+1, elementData, index,
                          numMoved);
     elementData[--size] = null; // clear to let GC do its work
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值