Java HashMap merge() 方法

3020. 子集中元素的最大数量【力扣周赛 382】用哈希表统计元素个数使用

点击查看代码
class Solution {
    public int maximumLength(int[] nums) {
        Map<Long, Integer> cnt = new HashMap<>();
        for (int x : nums) {
            cnt.merge((long) x, 1, Integer::sum);
        }
        // while true:
        Integer c1 = cnt.remove(1L);
        int ans = c1 != null ? c1 - 1 | 1 : 0;
        // 奇数-1为偶数,跟1取或后加1;偶数减1为奇数,或运算后不变(答案必须为奇数)
        for (long x : cnt.keySet()) {
            int res = 0;
            for (; cnt.getOrDefault(x, 0) > 1; x *= x) {
                res += 2;
            }
            res = res + (cnt.containsKey(x) ? 1 : -1);
            ans = Math.max(ans, res);
        }
        return ans;
    }
}

感谢灵神,灵神题解,还使用了keySet()方法

merge()

点击查看代码
        String k = "key";
        HashMap<String, Integer> map = new HashMap<String, Integer>() {{
            put(k, 1);
        }};
        map.merge(k, 2, (oldVal, newVal) -> oldVal + newVal);
等价于:
点击查看代码
        String k = "key";
        HashMap<String, Integer> map = new HashMap<String, Integer>() {{
            put(k, 1);
        }};
        Integer newVal = 2;
        if(map.containsKey(k)) {
            map.put(k, map.get(k) + newVal);
        } else {
            map.put(k, newVal);
        }

jdk8源码和注释

点击查看代码
 @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;
        // 该key原来的节点对象
        if (size > threshold || (tab = table) == null ||
          //第一个if,判断是否需要扩容
            (n = tab.length) == 0)
            n = (tab = resize()).length;
        if ((first = tab[i = (n - 1) & hash]) != null) {
            // 第二个if,取出old Node对象
            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) {
          // 第三个if,如果 old Node 存在
            V v;
            if (old.value != null)
           // 如果old存在,算出新的val并写入old Node后返回。
                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) {
           //如果old不存在且传入的newVal不为null,则put新的key
            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;
    }

感谢前辈https://www.jianshu.com/p/68e6b30410b0

热门相关:花都保镖   最牛兵王   第一强者   绝代疯少   铁血大明