日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

集合框架总结

發(fā)布時(shí)間:2023/12/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 集合框架总结 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

? 2019作為新的一年開(kāi)始,我也著手面試的準(zhǔn)備。這篇的博客的主角集合--面試中都會(huì)出現(xiàn)的,所以今天特作此總結(jié),也算是復(fù)習(xí)的成果的一個(gè)展示。在查看了許多的博客和源碼后我決定將其分成3部分來(lái)總結(jié)。

三個(gè)部分分別是:集合的分類、各個(gè)集合的底層實(shí)現(xiàn)、集合方法的源碼實(shí)現(xiàn)

                                    集合的分類

  1.集合--?collection接口

?

??

先上個(gè)圖

collection作為Set, List, Queue三個(gè)接口的父接口,它定義了操作集合的公用方法,包括add(),remove()等方法。稍后會(huì)對(duì)這些常用的方法進(jìn)行源碼解讀。集合作為存儲(chǔ)數(shù)據(jù)的容器,它的作用有點(diǎn)像數(shù)據(jù)庫(kù)的作用,而接口則提供了操作這些數(shù)據(jù)的方法。

下面來(lái)看下每個(gè)集合的底層數(shù)據(jù)結(jié)構(gòu)是怎么實(shí)現(xiàn)的不廢話先上圖

在知道了集合的分類和底層實(shí)現(xiàn)后,在來(lái)看下最經(jīng)常使用的幾個(gè)集合的源碼

1、ArrarList作為最經(jīng)常使用的集合。

  • 類聲明如下:
  • public class ArrayList<E> extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, java.io.Serializable

    實(shí)現(xiàn)的后三個(gè)接口分別表示,該集合可以隨機(jī)訪問(wèn)(通過(guò)下標(biāo)訪問(wèn)),克隆,序列化和反序列化(把對(duì)象轉(zhuǎn)換為字節(jié)序列的過(guò)程稱為對(duì)象的序列化。把字節(jié)序列恢復(fù)為對(duì)象的過(guò)程稱 為

    對(duì)象的反序列化。)

    實(shí)現(xiàn)的第一個(gè)接口List則表示ArrayList可以使用List接口的所有方法。

      2.?類的成員屬性

    public class ArrayList<E> extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, java.io.Serializable {// 版本號(hào)private static final long serialVersionUID = 8683452581122892189L;// 缺省容量private static final int DEFAULT_CAPACITY = 10;// 空對(duì)象數(shù)組private static final Object[] EMPTY_ELEMENTDATA = {};// 缺省空對(duì)象數(shù)組private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};// 元素?cái)?shù)組transient Object[] elementData;// 實(shí)際元素大小,默認(rèn)為0private int size;// 最大數(shù)組容量private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; }

    說(shuō)明:類的屬性中核心的屬性為elementData,類型為Object[],用于存放實(shí)際元素,并且被標(biāo)記為transient,也就意味著在序列化的時(shí)候,此字段是不會(huì)被序列化的。

      3.類的構(gòu)造函數(shù)ArrayList(int)--初始化指定大小的集合

    public ArrayList(int initialCapacity) {if (initialCapacity > 0) { // 初始容量大于0this.elementData = new Object[initialCapacity]; // 初始化元素?cái)?shù)組} else if (initialCapacity == 0) { // 初始容量為0this.elementData = EMPTY_ELEMENTDATA; // 為空對(duì)象數(shù)組} else { // 初始容量小于0,拋出異常throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);}}

    說(shuō)明:指定elementData數(shù)組的大小,不允許初始化大小小于0,否則拋出異常。

    ArrayList()無(wú)參構(gòu)造函數(shù),當(dāng)初始化時(shí)會(huì)給一個(gè)默認(rèn)10的容量大小 /*** Constructs an empty list with an initial capacity of ten.*/public ArrayList() {this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;} ArrayList(Collection<? extends E> c) 參數(shù)為泛型的集合參數(shù),即傳入的參數(shù)必須是集合的子類 /*** Constructs a list containing the elements of the specified* collection, in the order they are returned by the collection's* iterator.** @param c the collection whose elements are to be placed into this list* @throws NullPointerException if the specified collection is null*/public ArrayList(Collection<? extends E> c) {
          // 將傳入的集合轉(zhuǎn)為數(shù)組elementData
    = c.toArray();if ((size = elementData.length) != 0) {// c.toArray might (incorrectly) not return Object[] (see 6260652)
          // 可能不返回object[]數(shù)組
    if (elementData.getClass() != Object[].class)
          // 拷貝數(shù)據(jù)到object[]數(shù)組elementData
    = Arrays.copyOf(elementData, size, Object[].class);} else {// replace with empty array.this.elementData = EMPTY_ELEMENTDATA;}}

    ?  4.add方法

    public boolean add(E e) {//對(duì)數(shù)組的容量進(jìn)行調(diào)整ensureCapacityInternal(size + 1); // Increments modCount!!elementData[size++] = e;return true; }//在指定位置添加一個(gè)元素 public void add(int index, E element) {
    // 數(shù)組下標(biāo)校驗(yàn)rangeCheckForAdd(index);
    //對(duì)數(shù)組的容量進(jìn)行調(diào)整ensureCapacityInternal(size + 1); // Increments modCount!!// 把index后的數(shù)據(jù)往后移一位,在index位置插入新元素System.arraycopy(elementData, index, elementData, index + 1,size - index);elementData[index] = element;size++; }//添加一個(gè)集合 public boolean addAll(Collection<? extends E> c) {//把該集合轉(zhuǎn)為對(duì)象數(shù)組Object[] a = c.toArray();int numNew = a.length;//增加容量ensureCapacityInternal(size + numNew); // Increments modCount//挨個(gè)向后遷移System.arraycopy(a, 0, elementData, size, numNew);size += numNew;//新數(shù)組有元素,就返回 truereturn numNew != 0; }//在指定位置,添加一個(gè)集合 public boolean addAll(int index, Collection<? extends E> c) {rangeCheckForAdd(index);Object[] a = c.toArray();int numNew = a.length;ensureCapacityInternal(size + numNew); // Increments modCountint numMoved = size - index;//原來(lái)的數(shù)組挨個(gè)向后遷移if (numMoved > 0)System.arraycopy(elementData, index, elementData, index + numNew,numMoved);//把新的集合數(shù)組 添加到指定位置System.arraycopy(a, 0, elementData, index, numNew);size += numNew;return numNew != 0; }

      5.數(shù)組容量調(diào)整

    public void ensureCapacity(int minCapacity) {int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)// 不是默認(rèn)的數(shù)組,說(shuō)明已經(jīng)添加了元素? 0// 默認(rèn)的容量 : DEFAULT_CAPACITY;if (minCapacity > minExpand) {//當(dāng)前元素個(gè)數(shù)比默認(rèn)容量大 ensureExplicitCapacity(minCapacity);} }private void ensureCapacityInternal(int minCapacity) {//還沒(méi)有添加元素if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {//最小容量取默認(rèn)容量和 當(dāng)前元素個(gè)數(shù) 最大值minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);}ensureExplicitCapacity(minCapacity); }private void ensureExplicitCapacity(int minCapacity) {modCount++;// 容量不夠了,需要擴(kuò)容if (minCapacity - elementData.length > 0)grow(minCapacity); }

      6.擴(kuò)容

    private void grow(int minCapacity) {int oldCapacity = elementData.length;// 1.5 倍 原來(lái)容量int newCapacity = oldCapacity + (oldCapacity >> 1);//如果當(dāng)前容量還沒(méi)達(dá)到 1.5 倍舊容量,就使用當(dāng)前容量,省的站那么多地方if (newCapacity - minCapacity < 0)newCapacity = minCapacity;//新的容量居然超出了 MAX_ARRAY_SIZEif (newCapacity - MAX_ARRAY_SIZE > 0)//最大容量可以是 Integer.MAX_VALUEnewCapacity = hugeCapacity(minCapacity);// minCapacity 一般跟元素個(gè)數(shù) size 很接近,所以新建的數(shù)組容量為 newCapacity 更寬松些elementData = Arrays.copyOf(elementData, newCapacity); }private static int hugeCapacity(int minCapacity) {if (minCapacity < 0) // overflowthrow new OutOfMemoryError();return (minCapacity > MAX_ARRAY_SIZE) ?Integer.MAX_VALUE :MAX_ARRAY_SIZE; }

    ? ? ? ? ? ? ? ? ? ? ? ?

    ?7.查詢和修改

    E elementData(int index) {return (E) elementData[index]; }//獲取 public E get(int index) {rangeCheck(index);//直接根據(jù)數(shù)組角標(biāo)返回元素,快的一比return elementData(index); }//修改 public E set(int index, E element) {rangeCheck(index);E oldValue = elementData(index);//直接對(duì)數(shù)組操作elementData[index] = element;//返回原來(lái)的值return oldValue; }

    8、刪除

    //根據(jù)位置刪除 public E remove(int index) {rangeCheck(index);modCount++;E oldValue = elementData(index);//挨個(gè)往前移一位int numMoved = size - index - 1;if (numMoved > 0)System.arraycopy(elementData, index+1, elementData, index,numMoved);//原數(shù)組中最后一個(gè)元素刪掉elementData[--size] = null; // clear to let GC do its workreturn oldValue; }//刪除某個(gè)元素 public boolean remove(Object o) {if (o == null) {//挨個(gè)遍歷找到目標(biāo)for (int index = 0; index < size; index++)if (elementData[index] == null) {//快速刪除 fastRemove(index);return true;}} else {for (int index = 0; index < size; index++)if (o.equals(elementData[index])) {fastRemove(index);return true;}}return false; }//內(nèi)部方法,“快速刪除”,就是把重復(fù)的代碼移到一個(gè)方法里 //沒(méi)看出來(lái)比其他 remove 哪兒快了 - - private void fastRemove(int index) {modCount++;int numMoved = size - index - 1;if (numMoved > 0)System.arraycopy(elementData, index+1, elementData, index,numMoved);elementData[--size] = null; // clear to let GC do its work }//保留公共的 public boolean retainAll(Collection<?> c) {Objects.requireNonNull(c);return batchRemove(c, true); }//刪除或者保留指定集合中的元素 private boolean batchRemove(Collection<?> c, boolean complement) {final Object[] elementData = this.elementData;//使用兩個(gè)變量,一個(gè)負(fù)責(zé)向后掃描,一個(gè)從 0 開(kāi)始,等待覆蓋操作int r = 0, w = 0;boolean modified = false;try {//遍歷 ArrayList 集合for (; r < size; r++)//如果指定集合中是否有這個(gè)元素,根據(jù) complement 判斷是否往前覆蓋刪除if (c.contains(elementData[r]) == complement)elementData[w++] = elementData[r];} finally {//發(fā)生了異常,直接把 r 后面的復(fù)制到 w 后面if (r != size) {System.arraycopy(elementData, r,elementData, w,size - r);w += size - r;}if (w != size) {// 清除多余的元素,clear to let GC do its workfor (int i = w; i < size; i++)elementData[i] = null;modCount += size - w;size = w;modified = true;}}return modified; }//清楚全部 public void clear() {modCount++;//并沒(méi)有直接使數(shù)組指向 null,而是逐個(gè)把元素置為空//下次使用時(shí)就不用重新 new 了for (int i = 0; i < size; i++)elementData[i] = null;size = 0; }

    9、indexof和lastindexof

    public boolean contains(Object o) {return indexOf(o) >= 0; }//遍歷,第一次找到就返回 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; }

    由于 ArrayList 不是同步的,所以在并發(fā)訪問(wèn)時(shí),如果在迭代的同時(shí)有其他線程修改了 ArrayList, fail-fast 的迭代器 Iterator/ListIterator 會(huì)報(bào) ConcurrentModificationException 錯(cuò)。
    因此我們?cè)诓l(fā)環(huán)境下需要外部給 ArrayList 加個(gè)同步鎖,或者直接在初始化時(shí)用 Collections.synchronizedList 方法進(jìn)行包裝:

    List list = Collections.synchronizedList(new ArrayList(...));

    ?

    由于第一次寫(xiě)總結(jié)博客,的確是中費(fèi)時(shí)費(fèi)力的工作,但也收獲頗多,如有不足還請(qǐng)輕噴。

    參考博客:

    https://blog.csdn.net/u011240877/article/details/52853989

    https://blog.csdn.net/u011518120/article/details/52026076

    https://www.cnblogs.com/leesf456/p/5308358.html

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/angleshoot/p/10399945.html

    總結(jié)

    以上是生活随笔為你收集整理的集合框架总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。