Java 集合類 Set,Map,List

先上一張Java集合的框架圖,便於參考

以下所有特性僅代表自JAVA 1.7

 

List

interface Iterable<E>
interface Collection<E> extends Iterable<E>
interface Queue<E> extends Collection<E>
interface Deque<E> extends Queue<E>
interface List<E>  extends Collection<E>
abstract AbstractCollection<E> implements Collection<E>
abstract AbstractList<E> extends AbstractCollection<E> implements List<E>
abstract AbstractSequentialList<E> extends AbstractList<E>


    ArrayList:

 ArrayList<E> extends AbstractList<E> implements List<E>
        1.使用對象數組Object[]存儲數據,初始化容量爲10
        2.由於使用對象數組Object[]存儲數據, 所以ArrayList並不適合操作(增添,刪除)大規模的數據,因爲會涉及到擴容(擴容過程中需要新建更大內存的對象)和刪除(數據刪除會涉及到數據移位)。此處擴容同樣使用淺拷貝   詳細參考

        3.因此ArrayLis僅適合少量的數據操作和大規模數據的遍歷操作。
    
    
    LinkedList:

LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>
        1.使用 Entry<E> (雙向鏈表結構)來存儲數據

class Entry<E>{
     E e;  //to store the data for this node
     Entry<E> next;
     Entry<E> previous;
}


        2.由於使用鏈表結構存儲數據,因此LinkedList適合操作(增刪)大規模數據。
        

 

Map

interface Map<K,V>
abstract abstractMap<K,V> extends Map<K,V>    


    HashMap: 基本原理

HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>    詳細參考文章


        1.Hashmap默認初始化容量爲16,負載因子默認0.75
        2.使用 Entry[] table (數組) 和 Entry<K,V>(單向鏈表) 來存儲數據

Entry<K,V>{
     final K key;
     V value;
     Entry<K,V> next;
     final int hash;
}


        3.HashMap將會自動擴容當內存容量大於 (Capacity * loadFactor)時
        4.擴容後的新 Capacity 將會是舊的容量的兩倍
        5.使用哈希碼 hashcode 數據在數組 (Entry[])table中的位置 所以 HashMap是無序的
        6.HashMap沒有使用同步機制,所以HashMap是線程不安全的
 

    LinkedHashMap:

LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>    詳細參考文章


        1.默認初始化容量爲16,負載因子默認0.75
        2.使用 Entry[] table (數組)和Entry<K,V>(雙向鏈表)來存儲數據

static class Entry<K,V> extends HashMap.Entry<K,V> {
        // These fields comprise the doubly linked list used for iteration.
        Entry<K,V> before, after;

	Entry(int hash, K key, V value, HashMap.Entry<K,V> next) {
            super(hash, key, value, next);
        }
}


        3.LinkedHashMap將會自動擴容當內存容量超過(Capacity * loadFactor)
        4.新容量Capacity將會是舊的容量的兩倍
        5.使用哈希碼hashcode來計算數據在數組 (Entry[])table中的位置,但是使用雙向鏈表來(doubly linked list) 存儲數據和記錄順序,所以LinkedHashMap是有序的。
        6.LinkedHashMap沒有使用同步機制,所以LinkedHashMap是線程不安全的

 

Set

interface Iterable<E>
interface Collection<E> extends Iterable<E>
interface Set<E> extends Collection<E>
abstract  AbstractCollection<E> implements Collection<E>
abstract  AbstractSet<E> extends AbstractCollection<E> implements Set<E>


    HashSet:

HashSet<E> extends AbstractSet<E> implements Set<E>
        1.使用HashMap<K,V>來存儲數據, 並且數據存儲在HashMap的Key裏面, 而HashMap的value 存儲了一個空的Object
        2.由於HashMap存儲的數據是無序的,而Set是通過HashMap存儲數據,因此Set存儲的數據也是無序的
        3.由於HashMap的 key 不能夠重複, 因此HashSet 不能存儲重複的數據,重複的數據會產生覆蓋。
        
        
    LinkedHashSet:

LinkedHashSet<E> extends HashSet<E> implements Set<E>    
        1.使用LinkedHashSet<E> 來存儲詩句, 並且數據存儲在LinkedHashMap的 Key中, 而LinkedHashMap的value 存儲了一個空的Object
        2.由於LinkedHashMap存儲數據是有序的, 因此LinkedHashSet也是有序的。
        3.由於LinkedHashMap的 key 不能夠重複,會覆蓋重複的內容, 因此LinkedHashSet 也不能存儲重複的數據。

 

 

發佈了84 篇原創文章 · 獲贊 14 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章