Java容器類框架概述(0)

Java容器類概述數組

Java容器有兩類框架,一類是Collection,一類是Map,下面經過兩張圖片來分析一下這兩種容器,之因此說是容器,是由於Map不屬於Collection,而是一個單獨的接口框架

Collection接口

Collection接口
Collection接口

思惟導圖中的標註黑體的是比較常見的集合,主要用Arraylist,LinkedList,HashSet,Collection繼承了Iterable接口ide

Collection的內部方法

Collection繼承關係
Collection繼承關係

Collection的內部方法
Collection的內部方法

這些方法都很常見,根據名字基本上都能知道具體的做用,因此但凡是實現了Collection的接口都可以使用這些方法。工具

Collection的實現類

List

源碼的註釋this

  • An ordered collection (also known as a sequence). The user of this
    interface has precise control over where in the list each element is
    inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
  • 意思是說List是一個有序的Collection,這個接口的使用者可以準確的控制他所插入的每個元素,使用者也可以根據他們的整數索引在List中查找元素。

常見的List的實現有ArrayList,LinkedList設計

Set

源碼的註釋3d

  • A collection that contains no duplicate elements. More formally, sets
    contain no pair of elements e1 and e2 such that
    e1.equals(e2), and at most one null element. As implied by
    its name, this interface models the mathematical set abstraction.
  • 一個沒有重複元素的集合,而且,set也不能存放兩個用equals方法比較相等的元素,最多能夠放入一個null值。就像他的名字同樣,這個接口模擬了數學中的集合。

常見的set集合有HastSetrest

Queue

源碼的註釋code

  • A collection designed for holding elements prior to processing.
    Besides basic operations,queues provide additional insertion, extraction, and inspection
    operations. Each of these methods exists in two forms: one throws
    an exception if the operation fails, the other returns a special
    value (either {@code null} or {@code false}, depending on the
    operation). The latter form of the insert operation is designed
    specifically for use with capacity-restricted {@code Queue}
    implementations; in most implementations, insert operations cannot
    fail.
  • 一種被設計用來能夠放置優先級元素的集合,除了基本的Collection操做之外,queue還提供另外的插入,提取和檢查操做。每個方法的執行結構存在兩種形式:一種是執行失敗會跑出一種異常,另一種是返回特定的結果:null值或者false,具體取決於操做的結果。後一種插入操做的返回形式是針對特定容量的實現,在queue的大多實現裏,插入操做是不會失敗的。

常見的queue的實現有BlockingQueue跟PriorityQueueorm

Comparable跟Comparator

這是Java在集合中提供的兩個接口,主要用來比較兩個元素和進行排序,若是隻是比較相同的兩個類,則均可以實現,不過仍是有些區別:

  • Comparator是在類的外部進行排序,Comparable是在類的內部進行排序
  • Comparator比較適合對於多個類進行排序,只須要實現一個Comparator就能夠,Comparable則須要在每一個類中實現Comparable接口

Collection的工具類

Collection提供了兩個工具類,一個是Collections,一個是Arrays

Collections

Collections提供的方法
Collections提供的方法

Arrays

Arrays提供的方法
Arrays提供的方法

Java源碼的命名都比較規範,經過名字基本上能看出用法,有些即時不可以看出來用法,點擊去看一下注釋,或者寫個小demo也能知道,經過這兩個工具類,實際上能夠簡化咱們隊集合跟數組的操做,例如排序,同步,求最大值,最小值等,感興趣的能夠看一下,文章圖片比較多,是由於以爲一圖勝千言,因此代碼就儘可能少貼嘍。

Map接口

Map接口
Map接口

思惟導圖中的標註黑體的是比較常見的集合,主要有HashedMap,LinkedHashMap,TreeMap。

Map的內部方法

Map的內部方法
Map的內部方法

源碼註釋:

  • An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
  • 一個可以將key應射成value的Objec,Map不能有重複的key,每一個key最多隻能對應一個value

Map的實現類

比較常見的是HashMap,LinkedHashMap

總結

上面大題就是Java的整個容器框架,分析地比較簡單,接下來主要是分析下常見的Java容器類的實現類,畢竟整個源碼比較複雜,不能面面俱到。主要是經過思惟導圖和IDEA生成的UML來進行分析,這樣會顯得整個思路比較清晰,不至於一頭鑽進源碼,質檢數目,不見森林。

相關文章
相關標籤/搜索