Java容器類概述
Java容器有兩類框架,一類是Collection,一類是Map,下面通過兩張圖片來(lái)分析一下這兩種容器,之所以說(shuō)是容器,是因?yàn)镸ap不屬于Collection,而是一個(gè)單獨(dú)的接口
Collection接口
思維導(dǎo)圖中的標(biāo)注黑體的是比較常見的集合,主要用Arraylist,LinkedList,HashSet,Collection繼承了Iterable接口
Collection的內(nèi)部方法
這些方法都很常見,根據(jù)名字基本上都能知道具體的作用,所以但凡是實(shí)現(xiàn)了Collection的接口都能夠使用這些方法。
Collection的實(shí)現(xiàn)類
List
源碼的注釋
- An ordered collection (also known as a <i>sequence</i>). 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. - 意思是說(shuō)List是一個(gè)有序的Collection,這個(gè)接口的使用者能夠準(zhǔn)確的控制他所插入的每一個(gè)元素,使用者也能夠根據(jù)他們的整數(shù)索引在List中查找元素。
常見的List的實(shí)現(xiàn)有ArrayList,LinkedList
Set
源碼的注釋
- A collection that contains no duplicate elements. More formally, sets
contain no pair of elements <code>e1</code> and <code>e2</code> such that
<code>e1.equals(e2)</code>, and at most one null element. As implied by
its name, this interface models the mathematical <i>set</i> abstraction. - 一個(gè)沒有重復(fù)元素的集合,并且,set也不能存放兩個(gè)用equals方法比較相等的元素,最多可以放入一個(gè)null值。就像他的名字一樣,這個(gè)接口模擬了數(shù)學(xué)中的集合。
常見的set集合有HastSet
Queue
源碼的注釋
- 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. - 一種被設(shè)計(jì)用來(lái)可以放置優(yōu)先級(jí)元素的集合,除了基本的Collection操作以外,queue還提供另外的插入,提取和檢查操作。每一個(gè)方法的執(zhí)行結(jié)構(gòu)存在兩種形式:一種是執(zhí)行失敗會(huì)跑出一種異常,另外一種是返回特定的結(jié)果:null值或者false,具體取決于操作的結(jié)果。后一種插入操作的返回形式是針對(duì)特定容量的實(shí)現(xiàn),在queue的大多實(shí)現(xiàn)里,插入操作是不會(huì)失敗的。
常見的queue的實(shí)現(xiàn)有BlockingQueue跟PriorityQueue
Comparable跟Comparator
這是Java在集合中提供的兩個(gè)接口,主要用來(lái)比較兩個(gè)元素和進(jìn)行排序,如果只是比較相同的兩個(gè)類,則都可以實(shí)現(xiàn),不過還是有些區(qū)別:
- Comparator是在類的外部進(jìn)行排序,Comparable是在類的內(nèi)部進(jìn)行排序
- Comparator比較適合對(duì)于多個(gè)類進(jìn)行排序,只需要實(shí)現(xiàn)一個(gè)Comparator就可以,Comparable則需要在每個(gè)類中實(shí)現(xiàn)Comparable接口
Collection的工具類
Collection提供了兩個(gè)工具類,一個(gè)是Collections,一個(gè)是Arrays
Collections
Arrays
Java源碼的命名都比較規(guī)范,通過名字基本上能看出用法,有些即時(shí)不能夠看出來(lái)用法,點(diǎn)擊去看一下注釋,或者寫個(gè)小demo也能知道,通過這兩個(gè)工具類,實(shí)際上可以簡(jiǎn)化我們隊(duì)集合跟數(shù)組的操作,例如排序,同步,求最大值,最小值等,感興趣的可以看一下,文章圖片比較多,是因?yàn)橛X得一圖勝千言,所以代碼就盡量少貼嘍。
Map接口
思維導(dǎo)圖中的標(biāo)注黑體的是比較常見的集合,主要有HashedMap,LinkedHashMap,TreeMap。
Map的內(nèi)部方法
源碼注釋:
- An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
- 一個(gè)能夠?qū)ey應(yīng)射成value的Objec,Map不能有重復(fù)的key,每個(gè)key最多只能對(duì)應(yīng)一個(gè)value
Map的實(shí)現(xiàn)類
比較常見的是HashMap,LinkedHashMap
總結(jié)
上面大題就是Java的整個(gè)容器框架,分析地比較簡(jiǎn)單,接下來(lái)主要是分析下常見的Java容器類的實(shí)現(xiàn)類,畢竟整個(gè)源碼比較復(fù)雜,不能面面俱到。主要是通過思維導(dǎo)圖和IDEA生成的UML來(lái)進(jìn)行分析,這樣會(huì)顯得整個(gè)思路比較清晰,不至于一頭鉆進(jìn)源碼,只見樹木,不見森林。