Java HashMap merge() 方法
  iJkBC38KcPfg 2024年02月26日 228 0

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

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年02月26日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   55   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   113   0   0 Java
  8s1LUHPryisj   2024年05月17日   48   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
iJkBC38KcPfg