java 集合总结
一、集合類型
(1)集合
List? Queue Set? Map
List? Queue Set 實(shí)現(xiàn)Collection接口
(2)Collections工具類
reverse(List list):反轉(zhuǎn)
shuffle(List list),隨機(jī)排序
sort(List list, Comparator c);定制排序,由Comparator控制排序邏輯
swap(List list, int i , int j),交換兩個(gè)索引位置的元素
rotate((List list, int distance),旋轉(zhuǎn)
binarySearch(List list, Object key), 對(duì)List進(jìn)行二分查找,返回索引,注意List必須是有序的
max(Collection coll, Comparator c),根據(jù)定制排序,返回最大元素,排序規(guī)則由Comparatator類控制。類比int min(Collection coll, Comparator c)
fill(List list, Object obj),用元素obj填充list中所有元素
frequency(Collection c, Object o),統(tǒng)計(jì)元素出現(xiàn)次數(shù)
indexOfSubList(List list, List target), 統(tǒng)計(jì)targe在list中第一次出現(xiàn)的索引,找不到則返回-1,類比int lastIndexOfSubList(List source, list target).
boolean replaceAll(List list, Object oldVal, Object newVal), 用新元素替換舊元素
emptyXXX(),返回一個(gè)空的只讀集合(這不知用意何在?)
singleXXX(),返回一個(gè)只包含指定對(duì)象,只有一個(gè)元素,只讀的集合。
unmodifiablleXXX(),返回指定集合對(duì)象的只讀視圖。
synchronizedXXX()等方法,來(lái)將集合包裝成線程安全的集合
二、List?
| ArrayList | 數(shù)組 | 空數(shù)組,10 | oldSize + oldSize/2 | |
| Vector | 數(shù)組 | 10,增量為0 | oldSize + oldSize | synchronized | 
| LinkedList | jdk6帶頭結(jié)點(diǎn)的雙向循環(huán)鏈表 jdk7不帶頭結(jié)點(diǎn)的雙向鏈表,增加兩個(gè)節(jié)點(diǎn)指針first、last指向首尾節(jié)點(diǎn) | |||
| CopyOnWriteArrayList | 數(shù)組 | 0 | ReentrantLock | 
三、Queue
(1)offer,add 區(qū)別:
一些隊(duì)列有大小限制,因此如果想在一個(gè)滿的隊(duì)列中加入一個(gè)新項(xiàng),多出的項(xiàng)就會(huì)被拒絕。
這時(shí)新的 offer 方法就可以起作用了。它不是對(duì)調(diào)用 add() 方法拋出一個(gè) unchecked 異常,而只是得到由 offer() 返回的 false。
(2)poll,remove 區(qū)別:
remove() 和 poll() 方法都是從隊(duì)列中刪除第一個(gè)元素。remove() 的行為與 Collection 接口的版本相似, 但是新的 poll() 方法在用空集合調(diào)用時(shí)不是拋出異常,只是返回 null。因此新的方法更適合容易出現(xiàn)異常條件的情況。
(3)peek,element區(qū)別:
element() 和 peek() 用于在隊(duì)列的頭部查詢?cè)亍Ec remove() 方法類似,在隊(duì)列為空時(shí), element() 拋出一個(gè)異常,而 peek() 返回 null。
| PriorityQueue | 數(shù)組,comparator | 11 | oldCapacity + ((oldCapacity < 64) ? ? (oldCapacity + 2) : (oldCapacity >> 1)) | |
| ConcurrentLinkedQueue | 鏈表 | CAS | ||
| DelayQueue | PriorityQueue | ReentrantLock Condition | ||
| ArrayBlockingQueue | 數(shù)組,是否fair | ReentrantLock、Empty和Full Condition | ||
| LinkedBlockingQueue | 鏈表 | take和put ReentrantLock、Empty和Full? Condition | ||
| LinkedTransferQueue | 鏈表 | LockSupport | ||
| PriorityBlockingQueue | 數(shù)組 | oldCap + ((oldCap < 64) ? ?(oldCap + 2) :??(oldCap >> 1)) | ReentrantLock、Condition | |
| SynchronousQueue? | TransferQueue,TransferStack,是否fair | CAS | 
?Deque繼承Queue
| LinkedList | 鏈表 | |||
| ArrayDeque | 數(shù)組 | 8 | n << 1 | |
| ConcurrentLinkedDeque | 鏈表 | CAS | ||
| LinkedBlockingDeque | 鏈表 | Integer.MAX_VALUE | ReentrantLock、Empty和Full Condition | 
四、Map
| HashMap | ?jdk7 entry數(shù)組 + 鏈表 jdk8 Node數(shù)組 + 鏈表/紅黑樹 | 16 | table.size*2 | key、value可空 | |
| Hashtable | 數(shù)組 | 11 | table.size*2 + 1? | synchronized | key、value不可空 | 
| LinkedHashMap | 鏈表 | 記錄添加順序 | |||
| TreeMap | 紅黑樹,comparator | 默認(rèn)key不可空,排序? | |||
| EnumMap | key數(shù)組 value數(shù)組 enum.ordinal下標(biāo) | key不可空 ?value為空使用NULLObject填充 | |||
| ConcurrentHashMap | JDK7:Segment數(shù)組 + HashEntry數(shù)組 + 鏈表 JDK8:Node數(shù)組 + 鏈表 + 紅黑樹? | 分段鎖 ReentranLock、 synchronized和CAS | key、value不可空 | ||
| ConcurrentSkipListMap | 跳躍鏈表 | key、value不可空 | 
五、Set
| HashSet | HashMap | 
| LinkedHashSet | LinkedHashMap | 
| TreeSet | TreeMap | 
| ConcurrentSkipListSet | ConcurrentSkipListMap | 
| CopyOnWriteArraySet | CopyOnWriteArrayList | 
六、常見題目
(1)說(shuō)說(shuō)List,Set,Map三者的區(qū)別
 (2)Arraylist 與 LinkedList 區(qū)別
 RandomAccess接口、雙向鏈表和雙向循環(huán)鏈表
 (3)ArrayList 與 Vector 區(qū)別呢?為什么要用Arraylist取代Vector呢
 (4)說(shuō)一說(shuō) ArrayList 的擴(kuò)容機(jī)制吧
 (5)HashMap 和 Hashtable 的區(qū)別
 (6)HashMap 和 HashSet區(qū)別
 (7)HashSet如何檢查重復(fù)
 (8)HashMap的底層實(shí)現(xiàn)
 JDK1.8之前
 JDK1.8之后
 (9)HashMap 的長(zhǎng)度為什么是2的冪次方
 (10)HashMap 多線程操作導(dǎo)致死循環(huán)問(wèn)題
 (11)ConcurrentHashMap 和 Hashtable 的區(qū)別
 (12)ConcurrentHashMap線程安全的具體實(shí)現(xiàn)方式/底層具體實(shí)現(xiàn)
 JDK1.7、JDK1.8?
 (13)comparable 和 Comparator的區(qū)別
 Comparator定制排序、重寫compareTo方法實(shí)現(xiàn)排序
 (14)集合框架底層數(shù)據(jù)結(jié)構(gòu)總結(jié)
 Collection
 1. List
 2. Set
 Map
總結(jié)
 
                            
                        - 上一篇: 上市价 1199 元:三星 Buds L
- 下一篇: 最高立省三千元! 小米双十一科技狂欢季今
