java集合——集合框架
【0】README
0.1) 本文描述轉自 core java volume 1, 源代碼為原創,旨在理解 java集合——集合框架 的相關知識;
【1】集合框架
1.1) java集合類庫構成了集合類的礦建, 它為集合的實現者定義了大量的接口和抽象類。
1.1.1)集合框架的接口如下圖所示:
對上圖的分析(Analysis):A0)集合(Collection+Map)和集Set的區別:并不是所有的集合都是集,但所有的集都是集合;
- A1)集合有兩個基本接口: Collection 和 Map。 用add 方法插入集合;
- A2)使用 V put(K key, V value) 插入鍵值對到Map集合;V get(K key)從集合中讀取元素;(也需要使用迭代器訪問)
- A3)List 是一個有序集合。元素可以添加到 容器中某個特定的位置。
- A3.1)將對象放置到某個位置上有兩種方法: 使用整數索引或使用列表迭代器;
- A3.2)List 接口定義 了幾個用于隨機訪問的方法:
- A3.3)RandomAccess 接口: List 接口在提供這些隨機訪問方法時,并不管他們對某種特定實現是否高效。 為了避免執行成本較高的隨機訪問操作, java SE 1.4 引入了一個標記接口 RandomAccess;
-
- A3.4)這個接口沒有任何方法, 但可以用來檢測一個特定的集合是否支持高效的隨機訪問:(干貨——RandomAccess 接口的作用)
| * java.util Interface RandomAccess **All Known Implementing Classes:** ArrayList, AttributeList, CopyOnWriteArrayList, RoleList, RoleUnresolvedList, Stack, Vector public interface RandomAccess * Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists. * The best algorithms for manipulating random access lists (such as ArrayList) can produce quadratic behavior when applied to sequential access lists (such as LinkedList). Generic list algorithms are encouraged to check whether the given list is an instanceof this interface before applying an algorithm that would provide poor performance if it were applied to a sequential access list, and to alter their behavior if necessary to guarantee acceptable performance. * It is recognized that the distinction between random and sequential access is often fuzzy. For example, some List implementations provide asymptotically linear access times if they get huge, but constant access times in practice. Such a List implementation should generally implement this interface. As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, |
Annotation)
- A1)理論上說, 有一個獨立的Array接口, 它擴展于List接口, 并聲明了隨機訪問方法是很合理的;
- A2)但是,實際上,并沒有去定義這樣一個接口, 原因是 希望讓類庫的接口數量保持在一個較少的水平
1.2) ListIterator接口定義了一個方法,用于將一個元素添加到 迭代器所處位置的前面:void add(E element) , detailed Interface ListIterator , http://blog.csdn.net/pacosonswjtu/article/details/50302083
- 1.2.1)要想獲取和刪除給定位置的元素, 只需要調用 Iterator 接口中的next方法和 remove 方法即可;
1.3)Set接口與 Collection接口是一樣的,只不過其方法的行為有著更加嚴謹的定義:
- 1.3.1)集Set的add 方法拒絕添加重復的元素;
- 1.3.2)集Set的 equals 方法定義兩個集相等的條件是它們包含相同的元素但順序不必相同;
- 1.3.3) hashCode 方法定義應該保證具有相同元素的集合將會得到相同的散列碼;
- 1.3.4)既然方法簽名都相同了, 為什么還要建立一個獨立的接口呢?(干貨) 從概念上講,并不是所有集合都是集。 建立Set 接口后, 可以讓程序員編寫僅接受集的方法;
1.4) SortedSet 和 SortedMap 接口暴露了 用于排序的比較器對象, 并且定義的方法可以獲得集合的子集視圖;
1.5)最后, Java SE 6 引入了接口 NavigableSet 和 NavigatableMap, 其中包含了 幾個用于在有序集合映射表中查找和遍歷的方法(從理論上講, 這幾個方法以及包含在 SortedSet 和 SortedMap的接口中)。 TreeSet 和 TreeMap 類實現了這幾個接口;
1.6) 集合接口有大量的方法,這些方法可以通過更基本的方法加以實現。 抽象類提供了很多這樣的實例:
- 1.6.1)要知道, 抽象類實現了集合接口的部分方法, 其余方法由其子類實現;
- 1.6.2) 如果實現了自己的集合類,就可能要擴展上面某個類;
1.7)java 類庫支持下面幾種具體類:
LinkedList ArrayList ArrayDeque HashSet TreeSet PriorityQueue HashMap TreeMap- 1.7.1)下圖展示了這些類間的關系:
- 1.7.2)最后, 還有許多java 第一版留下來的 容器類(遺留下來的):
總結
以上是生活随笔為你收集整理的java集合——集合框架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 九月五号是什么星座 九月五号到底啥星座
- 下一篇: java集合——视图与包装器