HashMap<String, Boolean> mAchBools = new HashMap<String, Boolean>(){{
put("myBool1", false);
}
};
HashMap<String, Integer> mAchInts = new HashMap<String, Integer>(){{
put("myInt1", 0);
}
};
boolean isEmpty() {
for (boolean b : mAchBools.values()) if (b) return false;
for (int i : mAchInts.values()) if (i != 0) return false;
return true;
}
我创建了一个包含 boolean 值的 HashMap 和一个包含整数的 HashMap。现在我想要一个方法来检查值是否为“空”(所有 boolean 值均为 false,所有整数均为 0)。
我运行调试器,可以看到 Hashmap 中的 boolean 值为 false,但我的方法“isEmpty”仍然返回 false。
为什么我的“isEmpty”检查失败?我认为这是 boolean 值和 boolean 值之间的区别,但我不确定。
最佳答案
Java 自动在 Boolean <> boolean 和 int <> Integer 之间进行转换。因此,您的代码在技术上是正确的。
关于Java HashMap 迭代每个变量并返回值 : Boolean boolean , Integer int 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19410500/