日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java集合——集合框架

發布時間:2023/12/3 编程问答 62 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 接口定義 了幾個用于隨機訪問的方法:
void add(int index, E lement) E get(int index) void remove(int idnex)
  • 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,
this loop: for (int i=0, n=list.size(); i < n; i++)list.get(i); runs faster than this loop: for (Iterator i=list.iterator(); i.hasNext(); )i.next(); * This interface is a member of the Java Collections Framework. Since: 1.4 * 該標記接口被List實現類用來指示是否它們支持快速(通常是常數時間)隨機訪問。該接口的主要目的是 允許一般算法可以更改其行為以提供良好性能, 當該接口應用于隨機抑或是順序訪問列表的時候。 * 操縱隨機訪問列表(如ArrayList)的最佳算法是可以產生二次行為, 當它被應用于順序訪問列表的時候(如LinkedList)。 如果需要保證可接受的訪問性能,在一個已經被應用到順序訪問列表且性能不佳的算法被應用之前,鼓勵使用一般列表算法檢查是否給定列表是該接口的一個實例,; * 我們意識到,隨機訪問和順序訪問的區別是模糊的。舉個荔枝,一些List實現類提供趨于穩定地線性訪問時間,如果它們在實際應用中獲得一個較大且固定的訪問時間的話。這樣一個List實現類普遍應該實現該接口。 根據經驗,一個List 實現類應該實現這個接口,如果可能的話。 * 該類的一個典型荔枝: 循環: for (int i=0, n=list.size(); i < n; i++)list.get(i); 明顯快于下述循環:for (Iterator i=list.iterator(); i.hasNext(); )i.next();


if(c instanceof RandomAccess) {use random access alg } else {use sequential access alg }

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) 集合接口有大量的方法,這些方法可以通過更基本的方法加以實現。 抽象類提供了很多這樣的實例:

AbstractCollection AbstractList AbstractSequentialLIst AbstractSet AbstractQueue AbstractMap
  • 1.6.1)要知道, 抽象類實現了集合接口的部分方法, 其余方法由其子類實現;
  • 1.6.2) 如果實現了自己的集合類,就可能要擴展上面某個類;

1.7)java 類庫支持下面幾種具體類:

LinkedList ArrayList ArrayDeque HashSet TreeSet PriorityQueue HashMap TreeMap
  • 1.7.1)下圖展示了這些類間的關系:
  • 1.7.2)最后, 還有許多java 第一版留下來的 容器類(遺留下來的):
Vector + Stack + Hashtable + Properties

總結

以上是生活随笔為你收集整理的java集合——集合框架的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。