Map集合类用于存储元素对(key-value),其中每一个key映射到一个value,经过key来查找value。html
经常使用方法介绍:node
初始化一个map:数组
Map<key, value>map = new HashMap<key, value>();安全
map的大部分操做都是根据key来操做,key是惟一的。bash
HashMap是一个散列的、无序的key-value集合。HashSet的key只能为引用数据型,不能为基本数据类型,可使用基本数据类型的包装类,如Integer、Double等。数据结构
HashMap的存储结构是由数组和链表共同完成的。app
由上图可知,HashMap的Y轴方向是数组,X轴方向就是链表的存储方式。ui
一开始看put方法的源码真的是看的头晕了。。看了网上的解析,本身跟着从新读一遍源码。this
首先要重点关注的点有:spa
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}复制代码
//第一个参数hash:对key作了hash,若是key为null,hash=0;
//第2、三个参数分别为key和value
//第四个参数:若是为true,说明key相同时,不覆盖旧的value,false则覆盖value
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//第一次put的时候,数组为空,相似于初始化数组
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//节点要插入的数组位置(经过(数组长度-1)%hash肯定索引),若是为空的话,直接初始化node,并放在这个位置上
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
//该位置上有数据了
else {
Node<K,V> e; K k;
//判断第一个数据的hash、key和咱们要插入的是否相等
//若是相等,就取出该节点,e记录了该key相同的节点
if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//判断该节点是否红黑树,若是是红黑树,就用红黑树的方法插入
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
//若是不是红黑树,说明仍是链表
else {
for (int binCount = 0; ; ++binCount) {
//判断是否到链表尾,是就在表尾插入该节点
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//若是插入后链表长度>8,则把链表转换为红黑树
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//若是在遍历链表过程当中发现key相同的节点,就结束循环,e记录当前节点位置
if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
//若是e不为空,说明有key相同的节点
if (e != null) { // existing mapping for key
V oldValue = e.value;
//对该节点进行值覆盖,返回旧节点的值
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}复制代码
get方法的理解比put方法的理解简单。。
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}复制代码
//第一个参数,对key作hash,第二个参数为key
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
//数组不为空
//第一个节点不能为空
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
//判断第一个节点是否咱们要get的节点
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
//看当前节点是否属于红黑树,若是是,则用红黑树的方法去get
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
//遍历链表
do {
//若是找到key相同的节点,则返回节点
if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}复制代码
HashTable跟HashMap都实现了Map接口,但二者都几个小区别:
HashMap能够经过如下方法变成同步:
Map m = Collections.synchronizeMap(HashMap);
TreeMap是一个有序的k-v集合,是经过红黑树实现的,默认按照key的升序排序。
for(String s:map.keySet()){
System.out.println("key : "+s+" value : "+map.get(s));
}复制代码
for(String s1:map.keySet()){//遍历map的键
System.out.println("key :"+s1);
}
for(String s2:map.values()){//遍历map的值
System.out.println("value :"+s2);
}复制代码
for(Map.Entry<String, String> entry : map.entrySet()){
System.out.println("key :"+entry.getKey()+" value :"+entry.getValue());
}复制代码
Iterator<Map.Entry<String, String>> it=map.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, String> entry=it.next();
System.out.println("key :"+entry.getKey()+" value :"+entry.getValue());
}复制代码
参考资料: