} /** 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; }
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
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; }
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; }
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; // 访问后回调
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; }
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) { ### 标签:源码|集合 HashMap.Node...onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e);
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...onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess
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
但只能有一个 不保证元素是有序的,取决于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);
为原型创建一个新的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; }
value, false); } ConcurrentHashMap.Segment的put方法如下: final V put(K key, int hash, V value, boolean onlyIfAbsent...onlyIfAbsent) { e.value = value; ++modCount
它的签名如下: final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) 参数说明: hash:键的哈希值...onlyIfAbsent:是否仅在键不存在时才插入。 evict:是否在插入后进行驱逐操作。 该方法的返回值是插入前与键关联的旧值,如果没有旧值则返回null。...onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return
putVal(hash(key), key, value, false, true); } final V putVal(int hash, K key, V value, boolean onlyIfAbsent...onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e)...位置上有没有数据节点,如果没有则新建一个 如果bucket位置上存在数据节点,如果数据节点的hash值等于新数据的hash,并且数据节点的key和新数据的key相等,最终只是用新节点的Value替换原来节点Value(onlyIfAbsent...那么将采用红黑树的插入方式增加节点 如果不满足条件3并且节点不是一个TreeNode,则需要循环遍历链表中的节点,如果链表中的节点有和新的数据相等的节点,则直接退出,并且用新节点的Value替换原来节点的Value(onlyIfAbsent
领取专属 10元无门槛券
手把手带您无忧上云