publicHashMap(Map<?extendsK,?extendsV> m){this.loadFactor = DEFAULT_LOAD_FACTOR;putMapEntries(m,false);}========================================================================finalvoidputMapEntries(Map<?extendsK,?extendsV> m,boolean evict){//传入集合大小int s = m.size();if(s >0){if(table == null){// pre-sizefloat ft =((float)s / loadFactor)+1.0F;int t =((ft <(float)MAXIMUM_CAPACITY)?(int)ft : MAXIMUM_CAPACITY);if(t > threshold)
threshold =tableSizeFor(t);}elseif(s > threshold)resize();for(Map.Entry<?extendsK,?extendsV> e : m.entrySet()){
K key = e.getKey();
V value = e.getValue();putVal(hash(key), key, value,false, evict);}}}========================================================================//空参构造方法publicHashMap(){//0.75f负载因子赋值this.loadFactor = DEFAULT_LOAD_FACTOR;// all other fields defaulted}//空参构造方法(初始化容量)publicHashMap(int initialCapacity){this(initialCapacity, DEFAULT_LOAD_FACTOR);}========================================================================//int类型构造方法publicHashMap(int initialCapacity,float loadFactor){//判断容量initialCapacityif(initialCapacity <0)thrownewIllegalArgumentException("Illegal initial capacity: "+
initialCapacity);//判断容量最大值if(initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;if(loadFactor <=0|| Float.isNaN(loadFactor))thrownewIllegalArgumentException("Illegal load factor: "+
loadFactor);this.loadFactor = loadFactor;//判断容量最大值this.threshold =tableSizeFor(initialCapacity);}========================================================================staticfinalinttableSizeFor(int cap){int n = cap -1;
n |= n >>>1;
n |= n >>>2;
n |= n >>>4;
n |= n >>>8;
n |= n >>>16;return(n <0)?1:(n >= MAXIMUM_CAPACITY)? MAXIMUM_CAPACITY : n +1;}
2.put源码
//根据key获取hashcode值public V put(K key, V value){returnputVal(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)//初始化table
n =(tab =resize()).length;//i=元素在table存储的位置,赋值给pif((p = tab[i =(n -1)& hash])== null)//创建节点,存储
tab[i]=newNode(hash, key, value, null);else{//tab[i]有元素情况
Node<K,V> e; K k;//hash值相同if(p.hash == hash &&((k = p.key)== key ||(key != null && key.equals(k))))
e = p;//判断是不是树结构 jdk1.8提供红黑树优化方案elseif(p instanceofTreeNode)//基于红黑树插入
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 1st//如果大于则将链表转换成红黑树(优化查询性能)treeifyBin(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 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;}