Java的List集合框架之Vector詳細解析
(一)List子父層級:

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

