一.Map部分結(jié)構(gòu)圖

image.png
二.Map接口

image.png
三.AbstractMap抽象類
- 供子類實(shí)現(xiàn)的方法:put,entrySet
- Map的增刪改查都是通過(guò)獲取Iterator<Entry<K,V>> i = entrySet().iterator()遍歷實(shí)現(xiàn)的,以查詢?yōu)槔?/li>
public V get(Object key) {
Iterator<Entry<K,V>> i = entrySet().iterator();
if (key==null) {
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (e.getKey()==null)
return e.getValue();
}
} else {
while (i.hasNext()) {
Entry<K,V> e = i.next();
if (key.equals(e.getKey()))
return e.getValue();
}
}
return null;
}
- keySet()與values()
- keySet()是new 一個(gè)實(shí)現(xiàn)了迭代器的AbstractSet<K>()返回,values()是 new一個(gè)實(shí)現(xiàn)迭代器的AbstractCollection<V>()返回,因而可以使用迭代器遍歷map元素。
- 由于key和value存儲(chǔ)方式只實(shí)現(xiàn)各個(gè)線程內(nèi)存的可見性(volatile),但無(wú)法保證原子性。
transient volatile Set<K> keySet = null;
transient volatile Collection<V> values = null;
public Collection<V> values() {
if (values == null) {
values = new AbstractCollection<V>() {
public Iterator<V> iterator() {
return new Iterator<V>() {
private Iterator<Entry<K,V>> i = entrySet().iterator();
public boolean hasNext() {
return i.hasNext();
}
public V next() {
return i.next().getValue();
}
public void remove() {
i.remove();
}
};
}
return values;
}
四.SortedMap接口
- 相交Map附加方法:
- 進(jìn)一步提供關(guān)于鍵的總體排序 的 Map。該映射是根據(jù)其鍵的自然順序進(jìn)行排序的,或者根據(jù)在創(chuàng)建有序映射時(shí)提供的 Comparator 進(jìn)行排序。對(duì)有序映射的 collection 視圖(由 entrySet、keySet和 values 方法返回)進(jìn)行迭代時(shí),此順序就會(huì)反映出來(lái)。要采用此排序方式,還需要提供一些其他操作(此接口是 SortedSet 的對(duì)應(yīng)映射)。
image.png
五.NavigableMap接口
- 擴(kuò)展的 SortedMap,具有了針對(duì)給定搜索目標(biāo)返回最接近匹配項(xiàng)的導(dǎo)航方法。方法 lowerEntry、floorEntry、ceilingEntry 和 higherEntry 分別返回與小于、小于等于、大于等于、大于給定鍵的鍵關(guān)聯(lián)的 Map.Entry 對(duì)象,如果不存在這樣的鍵,則返回 null。類似地,方法 lowerKey、floorKey、ceilingKey和 higherKey只返回關(guān)聯(lián)的鍵。所有這些方法是為查找條目而不是遍歷條目而設(shè)計(jì)的
-
此接口還定義了 firstEntry、pollFirstEntry、lastEntry 和 pollLastEntry 方法,它們返回和/或移除最小和最大的映射關(guān)系(如果存在),否則返回 null
image.png
六.ConcurrentMap
- 提供其他原子 putIfAbsent、remove、replace方法的 Map。
image.png
七·.ConcurrentNavigableMap( extends ConcurrentMap<K,V>, NavigableMap<K,V>)
- 供其他原子 putIfAbsent、remove、replace方法的 Map;支持 NavigableMap 操作,且以遞歸方式支持其可導(dǎo)航子映射的 ConcurrentMap。
image.png



