我需要验证映射值是否为空,因为我经常使用集合,愿意实现验证映射值的通用方法,所以尝试了这个
public boolean checkNull(Map<String,Object> data){
if(data != null && !data.isEmpty()){
return true;
}
return false;
}
现在我只能使用键作为字符串和值作为对象来验证我的 map ,但我还需要使用任何类型的 map Map 或 Map.etc 检查此 null 条件, 那么我怎样才能做一个通用函数来验证这个 map 过程。
public boolean checkNull(Map<K,V> data){
if(data != null && !data.isEmpty()){
return true;
}
return false;
}
有人可以帮助我深入理解这一逻辑吗?
最佳答案
public boolean checkNull(Map<String,?> data){
// and you can also use this instead of the if:
return data != null && !data.isEmpty();
}
或
public boolean checkNull(Map<?,?> data){
取决于编译器已知的或应该检查的内容。
关于java - 如何验证通用 map 是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24606999/