Java容器學習之Collection

1)?容器之Collection

???Java中容器主要分為兩大類:Collection和Map,讓我們來看一下Collection下主要的繼承類和實現類。

Collection.jpg

???1.1)List:是一個有序的隊列,主要實現類如下圖所示。

List.jpg

???其中,ArrayList基于動態數組實現,支持隨機訪問,但是增加和刪除需要移動數據,是線程不安全的;LinkedList是以雙向鏈表實現,隨機訪問需要移動指針,增加刪除操作不需要移動數據,允許重復,是線程不安全的;Vector基于動態數組實現,是線程安全的。

???1.2)Queue:主要實現類如下圖所示。

Queue.jpg

???其中,PriorityQueue是基于堆結構實現,可以實現優先隊列,線程不安全;LinkedList可以實現雙向隊列。

???1.3)Set:是一種不允許數據元素重復的集合,無序,主要實現類及接口如下圖所示。

Set.png

???其中,HashSet實現Set接口,線程不安全,由哈希表(實際上是一個HashMap的實例)實現,底層是一個HashMap來保存HashSet中的所有元素,允許null值;LinkedHashSet繼承HashSet,源碼中其構造方法直接調用父類(HashSet)的構造方法,而父類的構造方法是HashMap實現所以LinkedHashSet也是HashMap實現的,線程不安全;

???這里要說一下TreeSet,它是AbstractSet接口的一個實現類,它的底層實現是TreeMap,而TreeMap的實現又借助了HashMap,所以,HashMap真的太重要了!TreeSet中元素是有序且不重復的,線程不安全,這里的有序是指按照關鍵字大小進行排序,實現原理是使用了比較器,源碼如下:

 /**
 * Constructs a new, empty tree set, sorted according to the specified
 * comparator.  All elements inserted into the set must be <i>mutually
 * comparable</i> by the specified comparator: {@code comparator.compare(e1,
 * e2)} must not throw a {@code ClassCastException} for any elements
 * {@code e1} and {@code e2} in the set.  If the user attempts to add
 * an element to the set that violates this constraint, the
 * {@code add} call will throw a {@code ClassCastException}.
 *
 * @param comparator the comparator that will be used to order this set.
 *        If {@code null}, the {@linkplain Comparable natural
 *        ordering} of the elements will be used.
 */
 public TreeSet(Comparator<? super E> comparator) {
    this(new TreeMap<>(comparator));
 }

???總結一下ArrayList和LinkedList的異同點:首先,兩者都是線程不安全的;ArrayList是基于動態數組實現的,LinkedList基于鏈表的數據結構;對于隨機訪問,ArrayList優于LinkedList,因為LinkedList要移動指針;對于增加和刪除操作,LinkedList要好一些。

???總結一下ArrayList和Vector的異同點:ArrayList線程不安全,而Vector是線程安全的;在進行擴容操作時,ArrayList每次resize為當前的1.5倍,而Vector每次擴大2倍(如下源碼所示)

???ArrayList擴容

private void grow(int minCapacity) {
    // overflow-conscious code
    int oldCapacity = elementData.length;
    //擴大為原來的1.5倍,因為oldCapacity >> 1 相當于 oldCapacity / 2
    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);
 }

???Vector擴容

 private void grow(int minCapacity) {
    // overflow-conscious code
    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);
  }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 學會記筆記的重要性 提起記筆記,很多人會覺得,記筆記是讀書時候的事了,現在工作了還有必要記筆記嗎?事實上,工作時候...
    安小拾閱讀 970評論 0 2
  • 在工作和生活中,我們經常會遇到各種各樣的問題和狀況,大到婚喪嫁娶,小到雞毛蒜皮,我們不可能處于沒有問題的真空狀態。...
    大鈞的健腦房閱讀 231評論 1 1
  • 晨霧蒙蒙曉穿松,稀稀落落幾顆星。 有心默默融一體,可惜頭上濕一叢。 早上我來到老山。天氣陰陰的,有點霧,也有點早。...
    andyany閱讀 415評論 0 0
  • 在教育孩子的問題上學會放下,溫柔以待。溺愛是包辦,愛是理解和接納。時時提醒自己,拿出真正的愛去愛孩子,這可能才是孩...
    微學廣思閱讀 177評論 0 0