Java 检查地图中的所有值是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22412017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if all values in a map are null
提问by Lluis Martinez
I need to check if all values in a map are null, I have this method that I want to replace by a built-in one if possible. Limitations: Java 5 and access to Apache Commons libraries.
我需要检查地图中的所有值是否都为空,如果可能的话,我想用内置方法替换这个方法。限制:Java 5 和对 Apache Commons 库的访问。
/* Checks if all values are null */
public static boolean isEmpty(Map<Dboid,?> aMap){
boolean isEmpty = true;
Iterator<?> it = aMap.entrySet().iterator();
while(it.hasNext() && isEmpty){
Object value = it.next();
if(value != null) {
isEmpty = false;
}
}
return isEmpty;
}
采纳答案by Rohit Jain
As such there is no direct method for this, but you can use Apache Commons CollectionUtils.countMatches()
method, and pass a NullPredicate
instance to it. Of course, you would do pass the values in the map using Map#values()
method:
因此,没有直接的方法,但您可以使用Apache CommonsCollectionUtils.countMatches()
方法,并将NullPredicate
实例传递给它。当然,您可以使用Map#values()
方法在地图中传递值:
public static <K, V> boolean hasAllNullValues(Map<K, V> map) {
int size = map.size();
return CollectionUtils.countMatches(map.values(), NullPredicate.INSTANCE) == size;
}
or even better, use CollectionUtils.exists()
method, to check there is at least one element that satisfies the NotNullPredicate
passed as second argument:
或者甚至更好,使用CollectionUtils.exists()
方法来检查至少有一个元素满足NotNullPredicate
作为第二个参数传递的:
public static <K, V> boolean hasAllNullValues(Map<K, V> map) {
return !CollectionUtils.exists(map.values(), NotNullPredicate.INSTANCE);
}
回答by Jason C
There is no built-in method to do this. In particular, there's nothing that provides a means of "finding an element that isn'tequal to something".
没有内置方法可以做到这一点。特别是,没有什么可以提供“找到不等于某物的元素”的方法。
However, if a map that contains only null
values is defined by your business rules to be "empty", that seems to imply that null
values mean "not present", in which case you may wish to construct the code such that null
values are never added in the first place. Then you can just use the built in isEmpty()
.
但是,如果null
您的业务规则将仅包含值的映射定义为“空”,这似乎意味着null
值意味着“不存在”,在这种情况下,您可能希望构造代码,以便null
永远不会添加值第一名。然后你就可以使用内置的isEmpty()
.
回答by Deian
There is no API that will give you that, however you could optimize that method a little bit.
No need to check the isEmpty variable on every iteration.
That is a minor optimization.
没有 API 可以提供给您,但是您可以稍微优化该方法。
无需在每次迭代时检查 isEmpty 变量。这是一个小的优化。
/* Checks if all values are null */
public static <K,V> boolean isMapEmpty(Map<K,V> aMap){
for (V v: aMap.values()) {
if (v != null) { return false; }
}
return true;
}
回答by Abhijeet Kushe
how about
怎么样
return CollectionUtils.find(aMap.values(),NotNullPredicate.INSTANCE).isEmpty();
回答by kapex
Another solution without using any third party libraries.
另一种不使用任何第三方库的解决方案。
Collections.frequency(aMap.values(), null) == aMap.size()
回答by PetroCliff
I know the question is for Java 5, but for those who will come here from google search as I did:
我知道这个问题是针对 Java 5 的,但是对于那些像我一样从谷歌搜索来到这里的人:
For Java >= 8 you can do:
对于 Java >= 8,您可以执行以下操作:
boolean allValuesAreNull = yourMap.values()
.stream()
.allMatch(Objects::isNull);
with one nuance: it will be true
for empty map.
有一个细微差别:它将true
用于空地图。