天天看點

JAVA集合體系詳解

因為不同的需求,Java提供了各種集合類,雖然它們的資料結構不同,但是它們卻有一些共性内容<比如:存儲,擷取,删除,判斷等。>,通過不斷的向上抽取,我們就能夠得到一個集合的繼承體系結構圖。

##一、Collection

Collection集合是單列集合的頂層根接口,是以在Collection中定義了所有單列集合通用的一些方法。它有兩個子接口,分别是List和Set。

###1. List接口

List接口的特點是元素有序、元素可重複,它可以對清單中每個元素的插入位置進行精确地控制,使用者可以根據元素的整數索引來通路、搜尋清單中的元素。

####Vector

Vector集合是JDK在1.0版本提供的線程安全的List接口實作類,底層通過Object數組實作,它有四個構造方法 :

① 無參構造:初始化長度為0,在無參構造方法中會調用int類型的帶參構造建立一個長度為10的Object類型數組,當這個數組存儲滿後會建立這個原數組長度2倍的新數組以擴容。

/**
     * Constructs an empty vector so that its internal data array
     * has size {@code 10} and its standard capacity increment is
     * zero.
     */
    public Vector() {
        this(10);
    }
           

② 一個int類型的帶參構造:在内部建立一個指定大小長度的Object類型數組用于存儲資料,當這個數組存儲滿後會建立這個原數組長度2倍的新數組以擴容。

/**
     * Constructs an empty vector with the specified initial capacity and
     * with its capacity increment equal to zero.
     *
     * @param   initialCapacity   the initial capacity of the vector
     * @throws IllegalArgumentException if the specified initial capacity
     *         is negative
     */
    public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }
           

③ 兩個int類型的帶參構造:在内部建立一個initialCapacity長度的Object類型數組用于存儲資料,當這個數組存儲滿後,如果capacityIncrement > 0,則會建立這個原數組長度加上capacityIncrement 的新數組以擴容,否則會建立這個原數組長度2倍的新數組以擴容。

/**
     * Constructs an empty vector with the specified initial capacity and
     * capacity increment.
     *
     * @param   initialCapacity     the initial capacity of the vector
     * @param   capacityIncrement   the amount by which the capacity is
     *                              increased when the vector overflows
     * @throws IllegalArgumentException if the specified initial capacity
     *         is negative
     */
    public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }
           

④ 一個Collection類型的帶參構造:根據給定的Collection集合實作類,建立一個Vector,當這個數組存儲滿後,會建立這個原數組長度2倍的新數組以擴容。

/**
     * Constructs a vector 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
     *       vector
     * @throws NullPointerException if the specified collection is null
     * @since   1.2
     */
    public Vector(Collection<? extends E> c) {
        elementData = c.toArray();
        elementCount = elementData.length;
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
    }
           

####ArrayList

ArrayList集合是JDK在1.2版本的時候提供的List接口實作類,底層也是通過Object數組實作,主要用于單線程通路場景,用來解決Vector由于線程同步帶來的效率相對低下問題。它有三個構造方法 :

① 無參構造:初始化長度為0,在第一次存儲元素的時候(add方法)會建立一個長度為10的新數組(類型為Object),當這個數組存儲滿後會建立這個原數組長度3/2的新數組以擴容。

/**
     * Constructs an empty list with an initial capacity of ten.
     */
    public ArrayList() {
        super();
        this.elementData = EMPTY_ELEMENTDATA;
    }
           

② 一個int類型的帶參構造:在内部建立一個指定大小長度的數組用于存儲資料,擴容同上。

/**
     * Constructs an empty list with the specified initial capacity.
     *
     * @param  initialCapacity  the initial capacity of the list
     * @throws IllegalArgumentException if the specified initial capacity
     *         is negative
     */
    public ArrayList(int initialCapacity) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
    }
           

③ 一個Collection類型的帶參構造:根據給定的Collection集合實作類,建立一個ArrayList,擴容同上。

/**
     * 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) {
        elementData = c.toArray();
        size = elementData.length;
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, size, Object[].class);
    }
           

####LinkedList

LinkedList集合也是JDK在1.2版本的時候提供的List接口實作類,底層通過雙向循環連結清單資料結構實作資料存儲,初始長度為0,即隻有一個表頭節點,前節點和後節點資訊均為null,用于表示一個空連結清單,沒有擴容因子,具有增删快,查詢慢且非線程安全的特點。内部通過一個Node實體類來實作連結清單式的資料存儲,取出元素的時候,如果角标index > size/2,那麼就從後向前依次周遊LinkedList所有元素,反之如果角标index < size/2,則從開頭依次周遊所有元素,直到index位置元素被周遊到并傳回,這也是查詢慢的原因。有兩個構造方法 :

① 無參構造:初始化長度為0。

/**
     * Constructs an empty list.
     */
    public LinkedList() {}
           

② 一個Collection類型的帶參構造:根據給定的Collection集合實作類,建立一個LinkedList。

/**
     * 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 LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }
           

④⑤⑥⑦⑧⑨⑩

快捷鍵

  • 加粗

    Ctrl + B

  • 斜體

    Ctrl + I

  • 引用

    Ctrl + Q

  • 插傳入連結接

    Ctrl + L

  • 插入代碼

    Ctrl + K

  • 插入圖檔

    Ctrl + G

  • 提升标題

    Ctrl + H

  • 有序清單

    Ctrl + O

  • 無序清單

    Ctrl + U

  • 橫線

    Ctrl + R

  • 撤銷

    Ctrl + Z

  • 重做

    Ctrl + Y

Markdown及擴充

Markdown 是一種輕量級标記語言,它允許人們使用易讀易寫的純文字格式編寫文檔,然後轉換成格式豐富的HTML頁面。 —— [ 維基百科 ]

使用簡單的符号辨別不同的标題,将某些文字标記為粗體或者斜體,建立一個連結等,詳細文法參考幫助?。

本編輯器支援 Markdown Extra ,  擴充了很多好用的功能。具體請參考Github.

表格

Markdown Extra 表格文法:

項目 價格
Computer $1600
Phone $12
Pipe $1

可以使用冒号來定義對齊方式:

項目 價格 數量
Computer 1600 元 5
Phone 12 元 12
Pipe 1 元 234

###定義清單

Markdown Extra 定義清單文法:

項目1

項目2

: 定義 A

: 定義 B

項目3
定義 C
定義 D
定義D内容

代碼塊

代碼塊文法遵循标準markdown代碼,例如:

@requires_authorization
def somefunc(param1='', param2=0):
    '''A docstring'''
    if param1 > param2: # interesting
        print 'Greater'
    return (param2 - param1 + 1) or None
class SomeClass:
    pass
>>> message = '''interpreter
... prompt'''
           

###腳注

生成一個腳注1.

目錄

[TOC]

來生成目錄:

文章目錄

    • ④⑤⑥⑦⑧⑨⑩
    • 快捷鍵
    • Markdown及擴充
      • 表格
      • 代碼塊
      • 目錄
      • 數學公式
      • UML 圖:
    • 離線寫部落格

數學公式

使用MathJax渲染LaTex 數學公式,詳見math.stackexchange.com.

  • 行内公式,數學公式為: Γ ( n ) = ( n − 1 ) ! ∀ n ∈ N \Gamma(n) = (n-1)!\quad\forall n\in\mathbb N Γ(n)=(n−1)!∀n∈N。
  • 塊級公式:

x = − b ± b 2 − 4 a c 2 a x = \dfrac{-b \pm \sqrt{b^2 - 4ac}}{2a} x=2a−b±b2−4ac

繼續閱讀