Java集合總結

前面已經把 Java Collections Framework整體過了一遍,動態數組ArrayList,樹集合TreeSet,雙向隊列LinkedList,鍵值對集合HashMap,樹集TreeMap。他們都各自有各自的優點,ArrayList動態擴容,數組實現查詢非常快但要求連續內存空間,TreeSet可以實現根據自然順序排序的無重複集合,其底層實現基於TreeMap,雙向隊列LinkedList不需要像ArrayList一樣創建連續的內存空間,它以鏈表的形式連接各個節點,但是查詢搜索效率極低。HashMap存放鍵值對,內部使用數組加鏈表實現,檢索快但是由於鍵是按照Hash值存儲的,所以無序,在某些情況下不合適。TreeMap使用優化了的排序二叉樹(紅黑樹)作爲邏輯實現,物理實現使用一個靜態內部類Entry代表一個樹節點,這是一個完全有序的結構,但是每個樹節點都需要保存一個父節點引用,左右孩子節點引用,還有一個value值,雖然效率高但開銷很大。

常見問題:

1.ArrayList和LinkedList的區別?

LikedList底層實現爲元素列表,每個節點都有前驅元素和後驅元素連接,插入、刪除和增加速度很快,時間複雜度爲O(1),查找的話,最壞情況需要遍歷列表,時間複雜度爲O(n)
ArrayList底層實現爲數組,可以通過索引取到對應位置的元素,查詢時間複雜度爲O(n),插入刪除和增加元素需要移動索引,時間負責度爲O(n)

2.ArrayList和Vector的區別?
vector保證同一時間只有一個線程可以訪問它,但是會有一些額外的資源開銷,一般情況推薦使用ArrayList,多線程情況下可以使用 Collections.synchronizedList來創建線程安全的ArrayList,效果等效於Vector。

As the documentation says, a Vector and an ArrayList are almost equivalent. The difference is that access to a Vector is synchronized, whereas access to an ArrayList is not. What this means is that only one thread can call methods on a Vector at a time, and there’s a slight overhead in acquiring the lock; if you use an ArrayList, this isn’t the case. Generally, you’ll want to use an ArrayList; in the single-threaded case it’s a better choice, and in the multi-threaded case, you get better control over locking. Want to allow concurrent reads? Fine. Want to perform one synchronization for a batch of ten writes? Also fine. It does require a little more care on your end, but it’s likely what you want. Also note that if you have an ArrayList, you can use the Collections.synchronizedList function to create a synchronized list, thus getting you the equivalent of a Vector

3.List和Set接口的區別?
List允許重複元素,可以插入一個或者多個null值。Set不允許插入重複元素(可用於去重),最多允許插入一個null值。
List是一個有序容器,Set是一個無序容器。

4.ListIterator和Iterator的區別?
Iterator只能向後遍歷, ListIterator可以向前向後遍歷,Iterator迭代時不能修改集合的元素, ListIterator可以修改集合中的元素.

5.LinkedList的getFirst()、getLast()和peekFirst()、peekLast()有什麼區別?
get系列的方法(JDK 1.2),當list爲空時會報NoSuchElementException,這會造成編程開發的不便(需要捕捉異常)。peek系列的方法(JDK 1.6)不會報這一錯誤。
參考:What is the difference between getFirst() and peekFirst() in Java’s LinkedList?

6.爲什麼LinkedList的peek()和peekFirst()以及pool()和pollFirst()具有同樣的功能?

LinkedList implements two interfaces - Queue and Deque. And Deque extends from Queue. Now, Deque has defined the method - Deque#pollFirst() and inherited the method - Queue#poll(). So, LinkedList has basically these two methods defined for the two interfaces it implements.

7.HashSet、LinkedHashSet和TreeSet的區別?

HashSet is Implemented using a hash table. Elements are not ordered. The add, remove, and contains methods have constant time complexity O(1).

HashSet的底層是哈希表,內部的元素是無序的,添加刪除和查詢操作的算法時間複雜度爲O(1).

LinkedHashSet is between HashSet and TreeSet. It is implemented as a hash table with a linked list running through it, so it provides the order of insertion. The time complexity of basic methods is O(1).

LinkedHashSet的底層也是哈希表,根據元素的hashCode決定它們的存儲位置,但是它使用鏈表維護元素的存儲位置,以元素的添加順序訪問集合的元素,基本的操作算法時間複雜度爲O(1).

TreeSet is implemented using a tree structure(red-black tree in algorithm book). The elements in a set are sorted, but the add, remove, and contains methods has time complexity of O(log (n)). It offers several methods to deal with the ordered set like first(), last(), headSet(), tailSet(), etc.

樹集合的底層是紅黑樹結構,元素順序默認按照自然順序排列,也可以自定義排序比較器,但是樹集合的基本操作算法時間複雜度爲O(log (n))。

8.HashMap、TreeMap 、 Hashtable和LinkedHashMap的區別?

HashMap is implemented as a hash table, and there is no ordering on keys or values.

HashMap按照key的hashCode存儲數據,內部數據是無序的,並且是非線程安全的,可以用Collections.synchronizedMap(HashMap map)方法使HashMap具有同步的能力。

TreeMap is implemented based on red-black tree structure, and it is ordered by the key.

TreeMap的key是根據紅黑樹存儲的,默認按照key的自然順序排序。

LinkedHashMap preserves the insertion order

LinkedHashMap保存了記錄的插入順序,在用Iteraor遍歷LinkedHashMap時,先得到的記錄肯定是先插入的。在遍歷的時候會比HashMap慢。有HashMap的全部特性。

Hashtable is synchronized, in contrast to HashMap. It has an overhead for synchronization

HashTable繼承了Java的Dictionary,基本功能和HashMap差不多,不過由於HashTable的所有方法都加了synchronized關鍵字,因此HashTable是線程安全的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章