java集合之Vector 詳解

Vector 是java 集合中的
他繼承了AbstractList 實現了List等接口
運用synchronized 保證了線程安全 但是也帶來了格外的性能開銷
默認擴容 1倍

	public class Vector<E>
    extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable

屬性解釋

// Vector 由object 數組構成
 protected Object[] elementData;
 // 數據個數
 protected int elementCount;
 // 每次Vector容量增加時的增量值 默認一倍
 protected int capacityIncrement;

構造方法

 	// 有參構造 傳入容量大小 和 capacityIncrement是每次Vector容量增加時的增量值
    public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0) 
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity]; // 直接new  initialCapacity 大小的object數組 
        this.capacityIncrement = capacityIncrement; // 容量自增量 賦值 默認爲0
    }
    // 只初始化容量大小 
     public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }
	// 無參構造 默認數組大小爲10
      public Vector() {
        this(10);
    }
    // 用一個 Vector c 初始化
	public Vector(Collection<? extends E> c) {
        elementData = c.toArray();
        elementCount = elementData.length; // 設置數組長度
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
    }

add函數解釋

	//add 增加一個 元素 套娃方法
   public synchronized boolean add(E e) {
        modCount++;// 結構修改次數加1
        add(e, elementData, elementCount);
        return true;
    }
    // 私有方法 被套娃
   private void add(E e, Object[] elementData, int s) {
        if (s == elementData.length) // 如果 數組存放元素的個數 等於數組的長度了 執行擴容
            elementData = grow();
        elementData[s] = e; // 增加
        elementCount = s + 1; // 數組元素個數+1
    }
    // 在index 位置 增加一個 元素
      public void add(int index, E element) {
        insertElementAt(element, index);
    }
    //在index位置 增加一個 Vector c
   public synchronized boolean addAll(int index, Collection<? extends E> c) {
        if (index < 0 || index > elementCount) // index 判斷是否符合
            throw new ArrayIndexOutOfBoundsException(index);

        Object[] a = c.toArray(); // 轉換成 array
        modCount++; // 修改次數加1
        int numNew = a.length;
        if (numNew == 0) // 如果a 的長度爲0 直接返回false
            return false;
        Object[] elementData = this.elementData;
        final int s = elementCount;
        if (numNew > elementData.length - s) // 如果 數組的空位 比你要插入的元素少 就執行擴容
            elementData = grow(s + numNew); // 擴容 傳遞 最小閾值

        int numMoved = s - index; 
        if (numMoved > 0)
            System.arraycopy(elementData, index,
                             elementData, index + numNew,
                             numMoved);
        System.arraycopy(a, 0, elementData, index, numNew);
        elementCount = s + numNew;
        return true;
    }
    // 將 集合 C 從末尾位置加入到Vector 
    public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray();
        modCount++;
        int numNew = a.length;
        if (numNew == 0)
            return false;
        synchronized (this) {
            Object[] elementData = this.elementData;
            final int s = elementCount;
            if (numNew > elementData.length - s)
                elementData = grow(s + numNew);
            System.arraycopy(a, 0, elementData, s, numNew);
            elementCount = s + numNew;
            return true;
        }
    }
    // 增加一個對象到Vectory 末尾
	public synchronized void addElement(E obj) {
        modCount++;
        add(obj, elementData, elementCount);
    }

indexof 方法

    // 0-size 之間
    public int indexOf(Object o) {
        return indexOf(o, 0);
    }
	// 對象o 在 index-size 之間 有沒有相同的元素 有返回 下標 否則返回 -1
	 public synchronized int indexOf(Object o, int index) {
        if (o == null) {
            for (int i = index ; i < elementCount ; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = index ; i < elementCount ; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

remove 方法

	// 根據索引 移除索引元素
	public synchronized E remove(int index) {
        modCount++; 
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);
        E oldValue = elementData(index); // 獲得這個元素值

        int numMoved = elementCount - index - 1;// 移除後新的數組大小
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
                             
        elementData[--elementCount] = null; // 讓垃圾收集

        return oldValue; // 返回index 元素值
    }
    // 套娃
	 public boolean remove(Object o) {
        return removeElement(o);
    }
    // 移除 集合c 的所有元素 
    public boolean removeAll(Collection<?> c) {
        Objects.requireNonNull(c); // objects 靜態方法 判斷 obj 是否爲null 等於null 拋出 NullPointerException
        return bulkRemove(e -> c.contains(e)); 
    }
    
private synchronized boolean bulkRemove(Predicate<? super E> filter) {
        int expectedModCount = modCount;
        final Object[] es = elementData;
        final int end = elementCount;
        int i;
    
        for (i = 0; i < end && !filter.test(elementAt(es, i)); i++)
            ;
        if (i < end) {
            final int beg = i;
            final long[] deathRow = nBits(end - beg);
            deathRow[0] = 1L;   // set bit 0
            for (i = beg + 1; i < end; i++)
                if (filter.test(elementAt(es, i)))
                    setBit(deathRow, i - beg);
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            modCount++;
            int w = beg;
            for (i = beg; i < end; i++)
                if (isClear(deathRow, i - beg))
                    es[w++] = es[i];
            for (i = elementCount = w; i < end; i++)
                es[i] = null;
            return true;
        } else {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            return false;
        }
    }

	// 移除所有元素
    .  public synchronized void removeAllElements() {
        final Object[] es = elementData;
        for (int to = elementCount, i = elementCount = 0; i < to; i++)
            es[i] = null;
        modCount++;
    }
    
    // 移除第一個對象
     public synchronized boolean removeElement(Object obj) {
        modCount++; 
        int i = indexOf(obj);//查找這個對象的位置 成功返回index  失敗返回-1
        if (i >= 0) {//成功
            removeElementAt(i); 
            return true;
        }
        return false;
    }
    // 移除索引元素 無返回值
     public synchronized void removeElementAt(int index) {
        if (index >= elementCount) { // 索引大於元素的長度
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                     elementCount);
        }
        else if (index < 0) {// 索引不符合規定
            throw new ArrayIndexOutOfBoundsException(index);
        }
        int j = elementCount - index - 1; // 假設是elementCount =10 index = 2  j= 10-2-1 =7
        if (j > 0) {//複製
            System.arraycopy(elementData, index + 1, elementData, index, j);
        }
        modCount++;
        elementCount--;
        elementData[elementCount] = null;
    }
    // 過濾移除
	public boolean removeIf(Predicate<? super E> filter) {
        Objects.requireNonNull(filter); 
        return bulkRemove(filter);
    }
    

擴容方法

	// 擴容
  private Object[] grow(int minCapacity) {
        return elementData = Arrays.copyOf(elementData,
                                           newCapacity(minCapacity));
    }
	// 默認擴容一倍
    private Object[] grow() {
        return grow(elementCount + 1);
    }
    // 新的擴容閾值
    private int newCapacity(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                         capacityIncrement : oldCapacity);
        if (newCapacity - minCapacity <= 0) {
            if (minCapacity < 0) // overflow
                throw new OutOfMemoryError();
            return minCapacity;
        }
        return (newCapacity - MAX_ARRAY_SIZE <= 0)
            ? newCapacity
            : hugeCapacity(minCapacity);
    }

其他方法

		//獲得index 索引的值
	    public synchronized E get(int index) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);

        return elementData(index);
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章