} /** Implementation for put and putIfAbsent */ final V putVal(K key, V value, boolean onlyIfAbsent...onlyIfAbsent) e.val = value;...onlyIfAbsent) p.val = value; }
param hash hash for key * @param key the key * @param value the value to put * @param onlyIfAbsent...return previous value, or null if none */ final V putVal(int hash, K key, V value, boolean onlyIfAbsent...onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess
final V put(K key, int hash, V value, boolean onlyIfAbsent) { HashEntry node = tryLock() ?...onlyIfAbsent) { e.value = value; ++modCount;...final V putVal(K key, V value, boolean onlyIfAbsent) { if (key == null || value == null) throw new...onlyIfAbsent) e.val = value; break...onlyIfAbsent) p.val = value; }
put(K key, V value) { return putVal(key, value, false);} final V putVal(K key, V value, boolean onlyIfAbsent...else { // 如果这个桶不为空且不在迁移元素,则锁住这个桶(分段锁) // 并查找要插入的元素是否在这个桶中 // 存在,则替换值(onlyIfAbsent...onlyIfAbsent) e.val = value; break;...= null) { // 如果找到了这个元素,则赋值了新值(onlyIfAbsent=false)...onlyIfAbsent) p.val = value; }
i) 要插入的元素在Node数组里 ii) 要插入的元素在TreeNode里 iii) 要插入的元素链表里 3) 根据onlyIfAbsent...param hash hash for key * @param key the key * @param value the value to put * @param onlyIfAbsent...* @return previous value, or null if none */ final V putVal(int hash, K key, V value, boolean onlyIfAbsent...= null) { // existing mapping for key, will replace the old value once onlyifAbsent is false...onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) { ### 标签:源码|集合 HashMap.Node...onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e);
key, V value) { return putVal(key, value, false); } final V putVal(K key, V value, boolean onlyIfAbsent...onlyIfAbsent) e.val = value;...onlyIfAbsent) p.val = value; }
0 : (h = key.hashCode()) ^ (h >>> 16); } final V putVal(int hash, K key, V value, boolean onlyIfAbsent...onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue;...final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node[] tab; Node...onlyIfAbsent || oldValue == null) https:// value覆盖旧Node的值 e.value = value; afterNodeAccess
为原型创建一个新的Segment对象,放入Segment数组并返回; 2、然后是执行Segment的put操作: final V put(K key, int hash, V value, boolean onlyIfAbsent...onlyIfAbsent) { e.value = value; ++modCount;...sizeCtl为-2时,说明进行树化操作; sizeCtl为-3时,说明当前key与函数做映射运算; put方法与初始化 final V putVal(K key, V value, boolean onlyIfAbsent...onlyIfAbsent) e.val = value; break...onlyIfAbsent) p.val = value; }
V value) { return putVal(key, value, false); } /** Implementation for put and putIfAbsent */ //onlyIfAbsent...的意思是在put一个KV时,如果K已经存在什么也不做则返回null //如果不存在则put操作后返回V值 final V putVal(K key, V value, boolean onlyIfAbsent...onlyIfAbsent) e.val = value; break;...onlyIfAbsent) p.val = value; }
方法,来看 JDK 1.7 中 ConcurrentHashMap 是如何保证线程安全的,具体实现源码如下: final V put(K key, int hash, V value, boolean onlyIfAbsent...ConcurrentHashMap 使用的是 CAS + volatile 或 synchronized 的方式来保证线程安全的,它的核心实现源码如下: final V putVal(K key, V value, boolean onlyIfAbsent...} else if ((fh = f.hash) == MOVED) tab = helpTransfer(tab, f); else if (onlyIfAbsent
但只能有一个 不保证元素是有序的,取决于hash后,在确定索引结果 add源码 //核心操作putVal final V putVal(int hash, K key, V value, boolean onlyIfAbsent...= null) { // 记录e的value V oldValue = e.value; // onlyIfAbsent为false...onlyIfAbsent || oldValue == null) //用新值替换旧值 e.value = value;
public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } // 第三个参数 onlyIfAbsent...,那么只有在不存在该 key 时才会进行 put 操作 // 第四个参数 evict 我们这里不关心 final V putVal(int hash, K key, V value, boolean onlyIfAbsent...onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e);
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {...// 记录e的value V oldValue = e.value; /** * onlyIfAbsent...onlyIfAbsent || oldValue == null) e.value = value; // 访问后回调
segmentShift) & segmentMask]; } 最后,在这个 Segment 中执行具体的 put 操作: V put(K key, int hash, V value, boolean onlyIfAbsent...onlyIfAbsent) e.value = value; // 替换 value 值 }
putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) 四个入参,分别为int hash是key计算出来的hash...值,key是要存入的键,value是要存入的值,onlyIfAbsent如果是true则不改变存在的值,默认为false,evict默认是true,如果是false则表处于创建模式; if ((tab
领取专属 10元无门槛券
手把手带您无忧上云