我有一个大小为 101 个键的 HashMap ,但我确信其中大约 6 个键内部没有数据,并且可能还有更多没有数据。空索引里面到底是什么?是否为空?或者是否有一个 Hash(index).isEmpty() 方法可以用来查看它是否为空?
我意识到 hashmap 中有一个 isEmpty 方法,但我认为它只检查整个映射是否为空,而不仅仅是单个索引。
最佳答案
I realize there is a isEmpty method inside hashmap, but I thought that only checked if the entire map was empty not just a single index.
我认为您正在寻找的是 containsKey(Object)
method 。根据文档:
Returns
true
if this map contains a mapping for the specified key. More formally, returnstrue
if and only if this map contains a mapping for a keyk
such that(key==null ? k==null : key.equals(k))
. (There can be at most one such mapping.)Parameters:
key
- key whose presence in this map is to be testedReturns:
true
if this map contains a mapping for the specified key
关于java - Hashmap 的空索引里面有什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3308782/