一 (*)Map接口
(9个方法)
(以key与value结构进行存储——>是数学中的函数 y=f(x))
(Map中的容器,元素是成对存在的每个元素由键与值两部分组成——>通过键可以找对所对应的值)
(常用的容器为HashMap,TreeMap)
(每个键只能对应一个值,值可以重复)
(用put方法添加元素)
1 Map接口介绍
Map接口定义了双例集合的存储特征,它并不是Collection接口的子接口。双例集合的存储特征是以key与value结构为单位进行存储。体现的是数学中的函数 y=f(x)感念。
Map与Collecton的区别:
- Collection中的容器,元素是孤立存在的(理解为单身),向集合中存储元素采用一个个元素的方式存储。
- Map中的容器,元素是成对存在的(理解为现代社会的夫妻)。每个元素由键与值两部分组成,通过键可以找对所对应的值。
- Collection中的容器称为单列集合,Map中的容器称为双列集合。
- Map中的集合不能包含重复的键,值可以重复;每个键只能对应一个值。
- Map中常用的容器为HashMap,TreeMap等。
2 Map接口中常用的方法表
方法 | 说明 |
---|---|
V put (K key,V value) | 把key与value添加到Map集合中 |
void putAll(Map m) | 从指定Map中将所有映射关系复制到此Map中(并集)相同会覆盖 |
V remove (Object key) | 删除key对应的value |
V get(Object key) | 根据指定的key,获取对应的value |
boolean containsKey(Object key) | 判断容器中是否包含指定的key |
boolean containsValue(Object value) | 判断容器中是否包含指定的value |
Set keySet() | 获取Map集合中所有的key,存储到Set集合中 |
Set<Map.Entry<K,V>> entrySet() | 返回一个Set基于Map.Entry类型包含Map中所有映射。 |
void clear() | 删除Map中所有的映射 |
二 (HashMap)
1 HashMap容器的使用
(基本都是Map接口里面的方法)
(底层采用了哈希表存储数据)
(键不能重复,如果发生重复,新的键值对会替换旧的键值对)
(在查找、删除、修改方面都有非常高的效率)
(HashTable类和HashMap用法几乎一样,就是被synchronized修饰,就是ArrayList和Vector的区别了)
HashMap采用哈希算法实现,是Map接口最常用的实现类。 由于底层采用了哈希表存储数据,我们要求键不能重复,如果发生重复,新的键值对会替换旧的键值对。 HashMap在查找、删除、修改方面都有非常高的效率。
具体使用
public class HashMapTest {public static void main(String[] args) {//实例化HashMap容器Map<String,String> map = new HashMap<>();//添加元素map.put("a","A");map.put("b","B");map.put("c","C");map.put("a","D");//获取容器中元素数量int size = map.size();System.out.println(size);System.out.println("---------------");//获取元素//方式一String v = map.get("a");System.out.println(v);System.out.println("---------------");//方式二Set<String> keys = map.keySet();for(String key:keys){String v1 = map.get(key);System.out.println(key+" ---- "+v1);}System.out.println("-------------------");//方式三Set<Map.Entry<String,String>> entrySet = map.entrySet();for(Map.Entry<String,String> entry:entrySet){String key = entry.getKey();String v2 = entry.getValue();System.out.println(key+" ---------- "+v2);}System.out.println("--------------------");//Map容器的并集操作Map<String,String> map2 = new HashMap<>();map2.put("f","F");map2.put("c","CC");map.putAll(map2);Set<String> keys2 = map.keySet();for(String key:keys2){System.out.println("key: "+key+" Value: "+map.get(key));}System.out.println("---------------");//删除元素String v3 = map.remove("a");System.out.println(v3);Set<String> keys3 = map.keySet();for(String key:keys3){System.out.println("key: "+key+" Value: "+map.get(key));}System.out.println("-------------------");//判断Key是否存在boolean b = map.containsKey("b");System.out.println(b);//判断Value是否存在boolean cc = map.containsValue("CC");System.out.println(cc);}
}
!!!面试:
HashTable类和HashMap用法几乎一样,底层实现几乎一样,只不过HashTable的方法添加了synchronized关键字确保线程同步检查,效率较低。
HashMap与HashTable的区别
+ HashMap: 线程不安全,效率高。允许key或value为null
+ HashTable: 线程安全,效率低。不允许key或value为null
2 源码——底层存储
(底层是哈希表本质——>数组+链表)
(链表到红黑树的转换)
HashMap底层实现采用了哈希表,这是一种非常重要的数据结构。对于我们以后理解很多技术都非常有帮助。
数据结构中由数组和链表来实现对数据的存储,他们各有特点。
(1) 数组:占用空间连续。 寻址容易,查询速度快。但是,增加和删除效率非常低。
(2) 链表:占用空间不连续。 寻址困难,查询速度慢。但是,增加和删除效率非常高。
那么,我们能不能结合数组和链表的优点(即查询快,增删效率也高)呢? 答案就是“哈希表”。 哈希表的本质就是“数组+链表”。
注意:
- 节点个数大于8井数组长度大于64 时,转换为红黑树——>提升效率
- 当节点个数小于6时转换为链表
3 源码——成员变量
-
数组默认初始化16,必须是2的次幂,以后算哈希值的时候会用到这个长度
-
数组的最大容量是2的30次幂
-
负载因子0.75——>决定数组在什么情况下扩容的(75%),(16—>12)
-
阈值8,当链表节点个数大于8时——>做红黑树的转换
-
阈值6,当红黑树长度小于6时——>转换成链表
-
阈值64,当数组长度大于64时——>做红黑树的转换
-
size返回容器个数
-
table数组(又叫桶)里面放链表
/*** The default initial capacity - MUST be a power of two.*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16/*** The maximum capacity, used if a higher value is implicitly specified* by either of the constructors with arguments.* MUST be a power of two <= 1<<30.*/
static final int MAXIMUM_CAPACITY = 1 << 30;/*** The load factor used when none specified in constructor.*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;/*** The bin count threshold for using a tree rather than list for a* bin. Bins are converted to trees when adding an element to a* bin with at least this many nodes. The value must be greater* than 2 and should be at least 8 to mesh with assumptions in* tree removal about conversion back to plain bins upon* shrinkage.*/
static final int TREEIFY_THRESHOLD = 8;/*** The bin count threshold for untreeifying a (split) bin during a* resize operation. Should be less than TREEIFY_THRESHOLD, and at* most 6 to mesh with shrinkage detection under removal.*/
static final int UNTREEIFY_THRESHOLD = 6;/*** The smallest table capacity for which bins may be treeified.* (Otherwise the table is resized if too many nodes in a bin.)* Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts* between resizing and treeification thresholds.*/
static final int MIN_TREEIFY_CAPACITY = 64;
/*** The number of key-value mappings contained in this map.*/
transient int size;/*** The table, initialized on first use, and resized as* necessary. When allocated, length is always a power of two.* (We also tolerate length zero in some operations to allow* bootstrapping mechanics that are currently not needed.)*/
transient Node<K,V>[] table;
4 源码——元素的节点类型
(Node类,TreeNode类,继承关系)
Node类
static class Node<K,V> implements Map.Entry<K,V> {final int hash; //哈希值final K key; //键V value; //值Node<K,V> next; //单向链表节点Node(int hash, K key, V value, Node<K,V> next) {this.hash = hash;this.key = key;this.value = value;this.next = next;}public final K getKey() { return key; }public final V getValue() { return value; }public final String toString() { return key + "=" + value; }public final int hashCode() {return Objects.hashCode(key) ^ Objects.hashCode(value);}public final V setValue(V newValue) {V oldValue = value;value = newValue;return oldValue;}public final boolean equals(Object o) {if (o == this)return true;if (o instanceof Map.Entry) {Map.Entry<?,?> e = (Map.Entry<?,?>)o;if (Objects.equals(key, e.getKey()) &&Objects.equals(value, e.getValue()))return true;}return false;}
}
TreeNode类——>(对红黑树的定义)
/*** Entry for Tree bins. Extends LinkedHashMap.Entry (which in turn* extends Node) so can be used as extension of either regular or* linked node.*/
static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {TreeNode<K,V> parent; //父节点TreeNode<K,V> left; //左子树TreeNode<K,V> right; //右子树TreeNode<K,V> prev; //前驱节点boolean red; //树的颜色TreeNode(int hash, K key, V val, Node<K,V> next) {super(hash, key, val, next);}/*** Returns root of tree containing this node.*/final TreeNode<K,V> root() {for (TreeNode<K,V> r = this, p;;) {if ((p = r.parent) == null)return r;r = p;}}
继承关系——>所以数组的类型是Node类型(因为TreeNode继承了Node)
5 源码——数组初始化
(用的是延迟初始化方式——>使用的时候初始化)
(resize方法——>实现数组初始化,和数组扩容)
(put方法里面——>putVal方法——>resize方法)
在JDK1.8的HashMap中对于数组的初始化采用的是延迟初始化方式。通过resize方法实现初始化处理。resize方法既实现数组初始化,也实现数组扩容处理。
/*** Initializes or doubles table size. If null, allocates in* accord with initial capacity target held in field threshold.* Otherwise, because we are using power-of-two expansion, the* elements from each bin must either stay at same index, or move* with a power of two offset in the new table.** @return the table*/
final Node<K,V>[] resize() {Node<K,V>[] oldTab = table;int oldCap = (oldTab == null) ? 0 : oldTab.length;int oldThr = threshold;int newCap, newThr = 0;if (oldCap > 0) {if (oldCap >= MAXIMUM_CAPACITY) {threshold = Integer.MAX_VALUE;return oldTab;}else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&oldCap >= DEFAULT_INITIAL_CAPACITY)newThr = oldThr << 1; // double threshold}else if (oldThr > 0) // initial capacity was placed in thresholdnewCap = oldThr;//核心代码 else { //第一次初始化执行else里面的代码newCap = DEFAULT_INITIAL_CAPACITY; //数组初始化长度16newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);//0.75*16}if (newThr == 0) {float ft = (float)newCap * loadFactor;newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?(int)ft : Integer.MAX_VALUE);}threshold = newThr; //12@SuppressWarnings({"rawtypes","unchecked"})//核心代码Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];//初始化长度为16的数组table = newTab;//把table赋值为16,最后返回if (oldTab != null) {for (int j = 0; j < oldCap; ++j) {Node<K,V> e;if ((e = oldTab[j]) != null) {oldTab[j] = null;if (e.next == null)newTab[e.hash & (newCap - 1)] = e;else if (e instanceof TreeNode)((TreeNode<K,V>)e).split(this, newTab, j, oldCap);else { // preserve orderNode<K,V> loHead = null, loTail = null;Node<K,V> hiHead = null, hiTail = null;Node<K,V> next;do {next = e.next;if ((e.hash & oldCap) == 0) {if (loTail == null)loHead = e;elseloTail.next = e;loTail = e;}else {if (hiTail == null)hiHead = e;elsehiTail.next = e;hiTail = e;}} while ((e = next) != null);if (loTail != null) {loTail.next = null;newTab[j] = loHead;}if (hiTail != null) {hiTail.next = null;newTab[j + oldCap] = hiHead;}}}}}return newTab;
}
6 源码——计算Hash值
(hash——>(散列)是一种算法——>为了能均匀的分布在数组当中)
(key对象的hashcode()方法——>hashcode值——>算出hash值——>[0, 数组长度-1]区间的整数)
(约定数组长度必须为2的整数幂——>hash值 = hashcode&(数组长度-1))
(哈希冲突——>退化成一个“链表”)
(!!!根据key的hashcode取它的高16位和hashcode的低16位去做一个异或运算,返回的结果再和数组的长度-1去做一个&运算,最后得到一个hash值,hash值决定数组存放在什么位置!!!)
HashMap中计算Hash值
-
获得key对象的hashcode
首先调用key对象的hashcode()方法,获得key的hashcode值。
-
根据hashcode计算出hash值(要求在[0, 数组长度-1]区间)hashcode是一个整数,我们需要将它转化成[0, 数组长度-1]的范围。我们要求转化后的hash值尽量均匀地分布在[0,数组长度-1]这个区间,减少“hash冲突”
-
一种极端简单和低下的算法是:
hash值 = hashcode/hashcode;
也就是说,hash值总是1。意味着,键值对对象都会存储到数组索引1位置,这样就形成一个非常长的链表。相当于每存储一个对象都会发生“hash冲突”,HashMap也退化成了一个“链表”。
-
一种简单和常用的算法是(相除取余算法):
hash值 = hashcode%数组长度;
这种算法可以让hash值均匀的分布在[0,数组长度-1]的区间。但是,这种算法由于使用了“除法”,效率低下。JDK后来改进了算法。首先约定数组长度必须为2的整数幂,这样采用位运算即可实现取余的效果:hash值 = hashcode&(数组长度-1)。
-
/*** Associates the specified value with the specified key in this map.* If the map previously contained a mapping for the key, the old* value is replaced.** @param key key with which the specified value is to be associated* @param value value to be associated with the specified key* @return the previous value associated with <tt>key</tt>, or* <tt>null</tt> if there was no mapping for <tt>key</tt>.* (A <tt>null</tt> return can also indicate that the map* previously associated <tt>null</tt> with <tt>key</tt>.)*/
public V put(K key, V value) {return putVal(hash(key), key, value, false, true);
}//hash(key)——>hash运算
static final int hash(Object key) {int h;return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
根据key的hashcode取它的高16位和hashcode的低16位去做一个异或运算
返回的结果再和数组的长度-1去做一个&运算,最后得到一个hash值
/*** Implements Map.put and related methods** @param hash hash for key* @param key the key* @param value the value to put* @param onlyIfAbsent if true, don't change existing value* @param evict if false, the table is in creation mode.* @return previous value, or null if none*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {Node<K,V>[] tab; Node<K,V> p; int n, i;if ((tab = table) == null || (n = tab.length) == 0)n = (tab = resize()).length;//真正做哈希运算的代码 if ((p = tab[i = (n - 1) & hash]) == null)tab[i] = newNode(hash, key, value, null);else {Node<K,V> e; K k;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);if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1sttreeifyBin(tab, hash);break;}if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))break;p = e;}}if (e != null) { // existing mapping for keyV oldValue = e.value;if (!onlyIfAbsent || oldValue == null)e.value = value;afterNodeAccess(e);return oldValue;}}++modCount;if (++size > threshold)resize();afterNodeInsertion(evict);return null;
}
7 源码——添加元素
(如果当前位置哈希不为空 ——>判断key是否相同,如果相同——>把新的value付给原来节点对象的value,把被覆盖的value返回 )
(key值不相同——>循环找到链表最后一个位置)
public V put(K key, V value) {return putVal(hash(key), key, value, false, true);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {//初始化的代码 Node<K,V>[] tab; Node<K,V> p; int n, i;if ((tab = table) == null || (n = tab.length) == 0)n = (tab = resize()).length;//真正做哈希运算的代码 if ((p = tab[i = (n - 1) & hash]) == null)tab[i] = newNode(hash, key, value, null);//如果当前位置哈希不为空 else {Node<K,V> e; K k;if (p.hash == hash && //判断key是否相同,相同就用新的value覆盖((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);//key值不相同 else {for (int binCount = 0; ; ++binCount) { //循环找到链表最后一个位置if ((e = p.next) == null) { //如果找到尾节点了p.next = newNode(hash, key, value, null); //创建节点对象,把值放里面if (binCount >= TREEIFY_THRESHOLD - 1) //如果节点数大于8了(前提数组长度大于64时)treeifyBin(tab, hash); //做链表到红黑树的转换break;}if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))break;p = e;}}//把新的value付给原来节点对象的value,把被覆盖的value返回 if (e != null) { // existing mapping for keyV oldValue = e.value;if (!onlyIfAbsent || oldValue == null)e.value = value;afterNodeAccess(e);return oldValue;}}++modCount;if (++size > threshold)resize();afterNodeInsertion(evict);return null;
}
8 源码——数组扩容
(是否对数组进行扩容的判断——>大于数组的负载因子(12))
(调用resize()方法——>执行里面的扩容操作(二倍扩容))
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {Node<K,V>[] tab; Node<K,V> p; int n, i;if ((tab = table) == null || (n = tab.length) == 0)n = (tab = resize()).length;if ((p = tab[i = (n - 1) & hash]) == null)tab[i] = newNode(hash, key, value, null);else {Node<K,V> e; K k;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);if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1sttreeifyBin(tab, hash);break;}if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))break;p = e;}}if (e != null) { // existing mapping for keyV oldValue = e.value;if (!onlyIfAbsent || oldValue == null)e.value = value;afterNodeAccess(e);return oldValue;}}----------------------------------------------------------------------------------------- //上面的之前都说过了 ++modCount; //计数if (++size > threshold) //是否对数组进行扩容的判断——>大于数组的负载因子(12)resize(); //调用方法,这里作用是扩容——>二倍大小扩容afterNodeInsertion(evict);return null;
}
final Node<K,V>[] resize() {Node<K,V>[] oldTab = table; //table默认长度16int oldCap = (oldTab == null) ? 0 : oldTab.length;//返回长度16 int oldThr = threshold; //负载因子12int newCap, newThr = 0; //初始化两个变量if (oldCap > 0) { //大于0,满足条件if (oldCap >= MAXIMUM_CAPACITY) { //是否大于数组上限threshold = Integer.MAX_VALUE; //如果到达上限无法扩容了return oldTab; }else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&//newCap16*2=32,小于数组上限oldCap >= DEFAULT_INITIAL_CAPACITY) //满足条件newThr = oldThr << 1; // double threshold //把负载因子12*2=24,赋给newThr}//不执行 else if (oldThr > 0) // initial capacity was placed in thresholdnewCap = oldThr;//初始化的代码也不执行 else { // zero initial threshold signifies using defaultsnewCap = DEFAULT_INITIAL_CAPACITY;newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);}//也不执行if (newThr == 0) {float ft = (float)newCap * loadFactor;newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?(int)ft : Integer.MAX_VALUE);}threshold = newThr; //threshold = 24 @SuppressWarnings({"rawtypes","unchecked"})Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; //创建Node类型新数组,长度32table = newTab; //把原来指向长度16的table覆盖if (oldTab != null) { //肯定不为空for (int j = 0; j < oldCap; ++j) { //下面所以代码是把数组长度为16Node<K,V> e; //的元素全部移动到长度为32里面if ((e = oldTab[j]) != null) { oldTab[j] = null; //代码最后返回newTab新长度32if (e.next == null)newTab[e.hash & (newCap - 1)] = e;else if (e instanceof TreeNode)((TreeNode<K,V>)e).split(this, newTab, j, oldCap);else { // preserve orderNode<K,V> loHead = null, loTail = null;Node<K,V> hiHead = null, hiTail = null;Node<K,V> next;do {next = e.next;if ((e.hash & oldCap) == 0) {if (loTail == null)loHead = e;elseloTail.next = e;loTail = e;}else {if (hiTail == null)hiHead = e;elsehiTail.next = e;hiTail = e;}} while ((e = next) != null);if (loTail != null) {loTail.next = null;newTab[j] = loHead;}if (hiTail != null) {hiTail.next = null;newTab[j + oldCap] = hiHead;}}}}}return newTab;
}