Java中的資料結構一覽

來源:互聯網
上載者:User

Java的類庫實在是很多,以至於很多人都不太瞭解,結果總是自己造輪子。

下面匯總了Java中的一些資料結構,加上一些實現的分析,同時備忘。

至於時間複雜度,個人覺得寫出來的用處不大。如果明白它是怎麼實現的,那自然就知道它的時間複雜度。

如果不理解它的實現,把時間複雜度背得再熟也沒用。


介面:

Collection<E>

子介面:

BlockingDeque<E>, BlockingQueue<E>, Deque<E>, List<E>, NavigableSet<E>, Queue<E>, Set<E>, SortedSet<E> 

實作類別:

ArrayBlockingQueue, ArrayDeque, ArrayList,  ConcurrentLinkedQueue, ConcurrentSkipListSet, CopyOnWriteArrayList, CopyOnWriteArraySet, DelayQueue, EnumSet, HashSet, LinkedBlockingDeque, LinkedBlockingQueue, LinkedHashSet, LinkedList, PriorityBlockingQueue, PriorityQueue, Stack, SynchronousQueue, TreeSet, Vector 


List<E>

實作類別:

ArrayList, CopyOnWriteArrayList, LinkedList,Stack, Vector 


Queue<E>

子介面:

BlockingDeque<E>, BlockingQueue<E>, Deque<E> 

實作類別:

ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, LinkedList, PriorityBlockingQueue, PriorityQueue, SynchronousQueue 


Set<E>

子介面:

NavigableSet<E>, SortedSet<E> 

實作類別:

ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, LinkedHashSet, TreeSet 


Map<K,V>

子介面:

ConcurrentMap<K,V>, ConcurrentNavigableMap<K,V>,  SortedMap<K,V> 

實作類別:

ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, TreeMap, WeakHashMap 



並發與安全執行緒等

通常含有Concurrent,CopyOnWrite,Blocking的是安全執行緒的,但是這些安全執行緒通常是有條件的,所以在使用前一定要仔細閱讀文檔。



具體實現:

List<E>系列:

ArrayList<E>,如其名,但是其容量增長計劃是newCapacity = (oldCapacity * 3)/2 + 1,和C++通常的Vector是翻倍的策略不同。

CopyOnWriteArrayList<E>,裡面有一個ReentrantLock,每當add時,都鎖住,把所有的元素都複製到一個新的數組上。

只保證曆遍操作是安全執行緒的,get操作並不保證,也就是說如果先得到size,再調用get(size-1),有可能會失效

那麼CopyOnWriteArrayList是如何?安全執行緒的迭代操作?

在迭代器中儲存原數組。

LinkedList<E>,標準雙向鏈表

Vector<E>,過時,多數方法上加上了synchronized

Stack<E>,繼承自Vector,過時,優先應使用 Deque<Integer> stack = new ArrayDeque<Integer>();


Queue<E>系列:

LinkedList<E>,見List<E>系列

ArrayDeque<E>,內部用一個數組儲存元素,有int類型head和tail的。

PriorityQueue<E>,內部用一個數組來儲存元素,但數組是以堆的形式來組織的,因此是有序的。

PriorityBlockingQueue<E>,封裝了一個PriorityQueue<E>,一個ReentrantLock,一個Condition,TODO

ArrayBlockingQueue<E>,TODO

ConcurrentLinkedQueue<E>,TODO

DelayQueue<E>,TODO

LinkedBlockingDeque<E>,TODO

LinkedBlockingQueue<E>,TODO

SynchronousQueue<E>,TODO


Deque<E>(雙端隊列)系列:

ArrayDeque<E>,見Queue系列

LinkedList<E>,見List系列

LinkedBlockingDeque<E>,TODO



Set系列:

HashSet,封裝了一個HashMap:

    public HashSet() {

        map = new HashMap<E,Object>();

    }

TreeSet,封裝了一個TreeMap,參考HashSet

LinkedHashSet,封裝了LinkedHashMap,參考HashSet

EnumSet,TODO

CopyOnWriteArraySet,簡單封裝了CopyOnWriteArrayList,注意這個Set的get的時間複雜度。

ConcurrentSkipListSet,封裝了一個ConcurrentSkipListMap,參考HashSet。



Map系列:

HashMap<K,V>,標準鏈地址法實現

TreeMap<K,V>,紅黑二叉樹

LinkedHashMap<K,V>,在Entry中增加before和after指標把HashMap中的元素串聯起來,這樣在迭代時,就可以按插入順序曆遍。

EnumMap,TODO

ConcurrentHashMap,參考之前的文章

ConcurrentSkipListMap,TODO,log(n)的時間複雜度,有點像多級鏈表儲存的,貌似有點像redis中的SortedSet的實現

Hashtable,過時

IdentityHashMap,正常的HashMap中比較是用equals方法,這個用的是“==”比較符

WeakHashMap<K,V>,弱引用的HashMap,正常的HashMap是強引用,即裡面的value不會被GC回收,在WeakHashMap<K,V>中,V中最好是WeakReference類型的,用像這樣的代碼:m.put(key, new WeakReference(value))。



其它的一些實用的第三方的資料結構:

LRUCache,LongHashMap,Java7中的LinkedTransferQueue,

Apache的包,裡面有很多實用的類:

http://commons.apache.org/collections/

Google的包,裡面有很多並發的牛B類:

AtomicLongMap,等等

大對象的資料結構:https://github.com/HugeCollections/Collections 

注意事項:

並發容器多數不能使用null值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.