Java的List集合框架之Vector詳細解析
(一)List子父層級:
- List接口繼承Collection,Collection繼承于Iterable;
- List接口實現類分為:Vector、ArrayList、LinkedList;
(二)List實現類——Vector
1、Vector實現類
(1)Vector底層是一個Object數組,protected Object[] elementData;
(2)Vector中的所有涉及到Object數組的均是會加synchronized,列舉常見方法:構造方法、add、remove、get、set等
(3)Vector中的默認容量為10,且擴容默認為當前容量翻倍(直接相加或者構造方法中指定擴容大小)。
2、常見源碼:
(1)構造方法:
//無參構造 public Vector() { this(10);//默認大小為10 }
//初始化指定大小的有參構造方法 public Vector(int initialCapacity) { this(initialCapacity, 0); }
//指定擴容大小和初始值的有參構造 public Vector(int initialCapacity, int capacityIncrement) { super();//父類無參構造 if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity];//實例化存儲數組 this.capacityIncrement = capacityIncrement;//設置自動擴容大小 }
(2)add方法:
/* * 此處列舉的直接末尾添加add方法,還有指定位置添加insertElementAt方法添加,操作數組均會在方法上添加synchronized鎖 **/ public synchronized boolean add(E e) { modCount++;//操作次數 ensureCapacityHelper(elementCount + 1);//是否需要擴容 elementData[elementCount++] = e;//存儲數據 return true; } //擴容判定 private void ensureCapacityHelper(int minCapacity) { if (minCapacity - elementData.length > 0)//判定是否需要擴容,當前存入值已大于數組大小 grow(minCapacity);//調用擴容方法 } //數組最大值,使用Integer最大值2147483648-8,因數組需要存自身的元數據占用8位(Class\Lock\Flag\Size) private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; //擴容方法 private void grow(int minCapacity) { int oldCapacity = elementData.length;//當前數組大小 //根據構造函數判定擴容值,未指定則采用翻一倍方式擴容 int newCapacity = oldCapacity + ((capacityIncrement > 0)?capacityIncrement : oldCapacity); if (newCapacity - minCapacity < 0)//判定新擴容值是否滿足當前存入后的數組大小 newCapacity = minCapacity;//擴容大小不滿足,則直接擴容到滿足存入后的數組大小 if (newCapacity - MAX_ARRAY_SIZE > 0)//判定擴容后的值是否大于數組最大值 newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity);//數組遷移復制 } //存儲最大值判定,超出限制則默認使用最大值 private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) //越界 throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE)?Integer.MAX_VALUE:MAX_ARRAY_SIZE;//返回最大值或者數組最大值 }
(3)remove方法:
/* * 此處列舉的返回boolean值的刪除方法,還有直接刪除指定索引的remove, * 它直接在方法上添加synchronized鎖,邏輯與具體刪除一致 **/ public boolean remove(Object o) { return removeElement(o);//調用具體刪除方法 } public synchronized boolean removeElement(Object obj) { modCount++;//操作次數 int i = indexOf(obj);//獲取obj在數組中存儲的下標,防止數組越界 if (i >= 0) { removeElementAt(i);//調用刪除方法 return true; } return false; } //索引尋找 public int indexOf(Object o) { return indexOf(o, 0);//傳入需要比對值和數組首標 } //遍歷數組尋找指定值(找尋下標,防止線程不安全,加synchronized) public synchronized int indexOf(Object o, int index) { if (o == null) {//判定傳入是否為空 for (int i = index ; i < elementCount ; i++) if (elementData[i]==null) return i;//返回數組下標第一個為null的索引 } else { for (int i = index ; i < elementCount ; i++) if (o.equals(elementData[i]))//判定需要刪除值與數組值是否相等,相等則返回第一個相等的下標 return i;//返回索引值 } return -1; } //根據索引對指定下標進行刪除(與存儲數組進行交互,添加synchronized鎖) public synchronized void removeElementAt(int index) { modCount++;//操作次數 if (index >= elementCount) {//越界判定 throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); }else if (index < 0) {//越界判定 throw new ArrayIndexOutOfBoundsException(index); } int j = elementCount - index - 1;//計算需要被遷移的數組成員數量 if (j > 0) { //使用系統(tǒng)類提供的數組復制方法進行遷移復制 //參數說明:被復制的數組,被復制的數組起點下標,目標數組(復制到的新數組),目標數組被復制的起點下標,需要復制的大小 System.arraycopy(elementData, index + 1, elementData, index, j); } elementCount--;//成員數量減1 //將原來存儲有值的最后一位值置為空,便于GC回收工作,提高內存使用率 elementData[elementCount] = null; }
(4)get方法:
public synchronized E get(int index) { if (index >= elementCount)//越界控制 throw new ArrayIndexOutOfBoundsException(index); return elementData(index); } //返回指定數組值 E elementData(int index) { return (E) elementData[index]; }
(5)set方法:
public synchronized E set(int index, E element) { if (index >= elementCount)//越界控制 throw new ArrayIndexOutOfBoundsException(index); E oldValue = elementData(index);//獲取索引已存在的值或者null elementData[index] = element;//設置新值 return oldValue;//返回設置之前的索引數組值 }
3、總結
(1)Vector是線程安全的,每個涉及到存儲數據的方法都添加synchronized鎖,性能相對會較低;
(2)Vector默認的存儲大小為10,不指定自動擴容大小,則默認使用當前數組大小進行自加擴容(直接相加或者構造方法中指定擴容大?。?
到此這篇關于Java的List集合框架之Vector詳細解析的文章就介紹到這了,更多相關List集合框架之Vector內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JAVA SpringBoot jar程序 Systemctl生產環(huán)境部署方案
這篇文章主要介紹了JAVA SpringBoot jar程序 Systemctl生產環(huán)境部署方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03SpringBoot整合Redis實現熱點數據緩存的示例代碼
這篇文章主要介紹了SpringBoot中整合Redis實現熱點數據緩存,本文以IDEA?+?SpringBoot作為?Java中整合Redis的使用?的測試環(huán)境,結合實例代碼給大家詳細講解,需要的朋友可以參考下2023-03-03