Java.util 源码阅读

util

AbstractCollection.java

//查询操作
public abstract Iterator<E> iterator();
public abstract int size();
public boolean isEmpty();
public boolean contains(Object o);
//toArray
public Object[] toArray();
public <T> T[] toArray(T[] a);
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private static <T> T[] finishToArray(T[] r,Iterator<?> it);
//Modification Operations
public boolean add(E e);
public boolean remove(Object o);
//Bulk Operation
public boolean containsAll(Collection<?> c);
public boolean addAll(Collection<? extends E> c);
public boolean removeAll(Collection<?> c);
public boolean retainAll(Collection<?> c);
public void clear();
//String conversion
public String toString();

以下为需要注意和没弄清楚的几点:

/*
 o == null 居然也可以返回true,在remove方法中采用了类似的处理方式
*/
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;
}
/*
1.Arrays.copyOf的用法
2.
*/
public Object[] toArrary(){
      Object[] r = new Object[size()];
      Iterator<E> it = iterator();
      for(int i=0;i<r.length;i++){
          if(!it.hasNext()){
              return Arrays.copyOf(r, i);             //源对象更短
          }
          r[i]=it.next();
      }
      return it.hasNext()? finishToArray(r,it):r;      //源对象更长?size()所取得长度小于it实际迭代获得的长度
}
/*
这部分没太看明白,找到了一个比较好的解释
https://blog.csdn.net/u014785687/article/details/78535435
*/
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a){
    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++){
        // 申请元素空间个数少于期待(size()返回)的个数
        if(!it.hasNext()){
     
            // a.length >= size
            if(a == r){
                r[i]= null;
            }
            // 这里可以认为i为a实际的大小,size为最开始确定的大小,然后由于同步发生了修改 出现了i与size不一致
            // a.length <size  且 a!= r
            // a.length < i <= size ,  即
            else if(a.length<i){
                return Arrays.copyOf(r, i);
            }
            // size > a.length >=i 
            else{
                System.arraycopy(r, 0, a, 0, i);
                // a.length > i
                if(a.length > i){
                    a[i]=null;
                }
            }
            return a;
        }
        r[i]=(T)it.next();
    }
    return it.hasNext()?finishToArray(r,it):r;
}

/*
需要扩容的情况
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

@SuppressWarnings("unchecked")
private static <T> T[] finishToArray(T[] r, Iterator<?> it){
    int i = r.length;
    while(it.hasNext()){
        int cap = r.length;
        if(i == cap){
            int newCap = cap + (cap >>1) +1;
            if(newCap - MAX_ARRAY_SIZE>0)
                newCap = hugeCapacity(cap+1);
            r = Arrays.copyOf(r,newCap);
        }
        r[i++]= (T)it.next();
    }
    return (i==r.length)? r:Arrays.copyOf(r, i);
}

private static int hugeCapacity(int minCapacity){
    if(minCapacity < 0){
        throw new OutOfMemoryError
        ("Required array size too large");
    }
    return( minCapacity >MAX_ARRAY_SIZE)?
    Integer.MAX_VALUE:
    MAX_ARRAY_SIZE;
}

AbstractList.java

/*
1.hashCode 为什么乘以31
https://blog.csdn.net/mxw2552261/article/details/91349677
*/
public int hashCode(){
    int hashCode = 1;
    for(E e :this)
        hashCode = 31*hashCode + (e==null?0:e.hashCode());
    return hashCode;
}
原文地址:https://www.cnblogs.com/zhouyu0-0/p/13418148.html