如果我这样做
System.out.println(nameOfaHashMap);
当该 map 为“空”时,系统会显示“{}
”。
在这种情况下,该映射是空的还是 null?
如果我愿意,我该怎么写:
if (nameOfanHashMap is != null)
{
System.out.println(nameOfaHashMap);
}
else System.out.println("I'm sorry, this map is empty!");
非常感谢大家,对不起我的英语和 Java :-) 回复的这么快,也真的很感谢大家。
我写了这段代码,没问题:
if (disponibilita.isEmpty())
{
System.out.println("Sorry, ..");
}
else
System.out.println(disponibilita);
最佳答案
如果它是null
,那么它甚至不是 map ;如果它是一张 map ,只有这样它才有可能为空。
空 map 的 toString
通常会返回 {}
,而 String.valueOf(null)
会返回字符串 "null"
,这就是打印的内容。
如果你想在有 map 的时候打印“对不起,这张 map 是空的”,但是它是空的,那么你需要
if (map.isEmpty()) System.out.println("sorry, this map is empty");
关于java - "{}"是什么意思?什么时候 map 是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20554829/