HashMap中的密钥存在检查

探讨了在频繁访问HashMap的情况下,如何通过直接捕获异常而非检查密钥存在性来提高效率,指出此方法虽非最佳编程实践,但在特定场景下可显著减少访问次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Key existence check in HashMap

Is checking for key existence in HashMap always necessary? 是否总是需要在HashMap中检查密钥是否存在?

I have a HashMap with say a 1000 entries and I am looking at improving the efficiency. 我有一个HashMap,说1000条目,我正在寻求提高效率。 If the HashMap is being accessed very frequently, then checking for the key existence at every access will lead to a large overhead. 如果非常频繁地访问HashMap,那么在每次访问时检查密钥是否存在将导致很大的开销。 Instead if the key is not present and hence an exception occurs, I can catch the exception. 相反,如果密钥不存在并因此发生异常,我可以捕获异常。 (when I know that this will happen rarely). (当我知道这种情况很少发生时)。 This will reduce accesses to the HashMap by half. 这将减少对HashMap的访问一半。

This might not be a good programming practice, but it will help me reduce the number of accesses. 这可能不是一个好的编程习惯,但它会帮助我减少访问次数。 Or am I missing something here? 或者我在这里遗漏了什么?

[ Update ] I do not have null values in the HashMap. [ 更新 ]我在HashMap中没有空值。


#1楼

参考:https://stackoom.com/question/FDU0/HashMap中的密钥存在检查


#2楼

if(map.get(key) != null || (map.get(key) == null && map.containsKey(key)))

#3楼

Do you ever store a null value? 你有没有存储空值? If not, you can just do: 如果没有,你可以这样做:

Foo value = map.get(key);
if (value != null) {
    ...
} else {
    // No such key
}

Otherwise, you could just check for existence if you get a null value returned: 否则,如果返回null值,则可以检查是否存在:

Foo value = map.get(key);
if (value != null) {
    ...
} else {
    // Key might be present...
    if (map.containsKey(key)) {
       // Okay, there's a key but the value is null
    } else {
       // Definitely no such key
    }
}

#4楼

I usually use the idiom 我通常使用这个成语

Object value = map.get(key);
if (value == null) {
    value = createValue(key);
    map.put(key, value);
}

This means you only hit the map twice if the key is missing 这意味着如果缺少密钥,则只会按两次地图


#5楼

Do you mean that you've got code like 你是说你有像这样的代码

if(map.containsKey(key)) doSomethingWith(map.get(key))

all over the place ? 到处都是 ? Then you should simply check whether map.get(key) returned null and that's it. 然后你应该简单地检查map.get(key)是否返回null,就是这样。 By the way, HashMap doesn't throw exceptions for missing keys, it returns null instead. 顺便说一下,HashMap不会为丢失的密钥抛出异常,而是返回null。 The only case where containsKey is needed is when you're storing null values, to distinguish between a null value and a missing value, but this is usually considered bad practice. 需要containsKey的唯一情况是当你存储空值时,要区分空值和缺失值,但这通常被认为是不好的做法。


#6楼

You won't gain anything by checking that the key exists. 通过检查密钥是否存在,您将无法获得任何收益。 This is the code of HashMap : 这是HashMap的代码:

@Override
public boolean containsKey(Object key) {
    Entry<K, V> m = getEntry(key);
    return m != null;
}

@Override
public V get(Object key) {
    Entry<K, V> m = getEntry(key);
    if (m != null) {
        return m.value;
    }
    return null;
}

Just check if the return value for get() is different from null . 只需检查get()的返回值是否与null不同。

This is the HashMap source code. 这是HashMap源代码。


Resources : 资源:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值