JavaSE---集合

Posted anpeiyong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaSE---集合相关的知识,希望对你有一定的参考价值。

1、【ArrayList】

 

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{
private static final long serialVersionUID = 8683452581122892189L; private static final int DEFAULT_CAPACITY = 10;   public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }   private void ensureCapacityInternal(int minCapacity) { ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); }   private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); }   private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; 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); }
  
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}

public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size-1; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = size-1; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}

}


 

    初始容量:10   扩容:为原来的一半 int newCapacity = oldCapacity + (oldCapacity >> 1);

 

2、【HashMap】

public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {
    
   static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
   static final int MAXIMUM_CAPACITY = 1 << 30;  
   static final float DEFAULT_LOAD_FACTOR = 0.75f;

}

    初始容量:16   































以上是关于JavaSE---集合的主要内容,如果未能解决你的问题,请参考以下文章

JavaSE——Stream流

JavaSE基础---集合之Set

JavaSE 一些技巧 02——遍历集合的方法总结

代码片段 - Golang 实现集合操作

laravel特殊功能代码片段集合

JavaSE——链表集合