文章目录

hashmap里面的这个merge方法,融合的意思,将新的value和旧的value进行融合。

@Override
public V merge(K key, V value,
               BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
    if (value == null)
        throw new NullPointerException();
    if (remappingFunction == null)
        throw new NullPointerException();
    int hash = hash(key);
    Node<K,V>[] tab; Node<K,V> first; int n, i;
    int binCount = 0;
    TreeNode<K,V> t = null;
    Node<K,V> old = null;
    if (size > threshold || (tab = table) == null ||
        (n = tab.length) == 0)
        n = (tab = resize()).length;
    if ((first = tab[i = (n - 1) & hash]) != null) {
        if (first instanceof TreeNode)
            old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
        else {
            Node<K,V> e = first; K k;
            do {
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k)))) {
                    old = e;
                    break;
                }
                ++binCount;
            } while ((e = e.next) != null);
        }
    }
    if (old != null) {
        V v;
        if (old.value != null)
            v = remappingFunction.apply(old.value, value);
        else
            v = value;
        if (v != null) {
            old.value = v;
            afterNodeAccess(old);
        }
        else
            removeNode(hash, key, null, false, true);
        return v;
    }
    if (value != null) {
        if (t != null)
            t.putTreeVal(this, tab, hash, key, value);
        else {
            tab[i] = newNode(hash, key, value, first);
            if (binCount >= TREEIFY_THRESHOLD - 1)
                treeifyBin(tab, hash);
        }
        ++modCount;
        ++size;
        afterNodeInsertion(true);
    }
    return value;
}

从其调用v = remappingFunction.apply(old.value, value);这个方法中可以看出来,初入的是old.value和value这两个值进行操作,所以你写的方法里面获取的两个值是这两个value进行操作的。

直接案例代码:

当map为null没存任何值时:

public static void main(String[] args) {
    Map<String, String> map = new HashMap<>();
    map.merge("j"," yi",(k,v)->genValue1(k,v));
    System.out.println("当map中没有任何值时:" +map);
}
static String genValue1(String k,String v){
    System.out.println(k+v);
    return k+v;
}
结果:
当map中没有任何值时:{j= yi}

当该key的value为null时:

public static void main(String[] args) {
    Map<String, String> map = new HashMap<>();
    map.put("j",null);
    map.merge("j"," yi",(k,v)->genValue1(k,v));
    System.out.println("当该key的value为null时:" +map);
}
static String genValue1(String k,String v){
    System.out.println(k+v);
    return k+v;
}
结果:
当该key的value为null时:{j= yi}

当存在该key且有值时:

public static void main(String[] args) {
    Map<String, String> map = new HashMap<>();
    map.put("j","xue");
    map.merge("j"," yi",(k,v)->genValue1(k,v));
    System.out.println("当存在该key且有值时:" +map);
}
static String genValue1(String k,String v){
    System.out.println(k+v);
    return k+v;
}
结果:
xue yi
当存在该key且有值时:{j=xue yi}

当进行数据操作时返回值为null:

public static void main(String[] args) {
    Map<String, String> map = new HashMap<>();
    map.put("j","xue");
    map.merge("j"," yi",(k,v)->genValue1(k,v));
    System.out.println("当进行数据操作时返回值为null:" +map);
}
static String genValue1(String k,String v){
    System.out.println(k+v);
    return null;
}
结果:
xue yi
当进行数据操作时返回值为null:{}