简体   繁体   English

这是什么意思? 地图何时为空?

[英]What does “{}” mean? When is a map empty?

If I do 如果我做

System.out.println(nameOfaHashMap);

when that map is "empty", the system says " {} ". 当该映射为“空”时,系统将显示“ {} ”。

In this case, is that map empty or null? 在这种情况下,地图是空的还是空的?

How can I write if I would: 如果我愿意怎么写:

if (nameOfanHashMap is != null)
{
    System.out.println(nameOfaHashMap);  
}
else System.out.println("I'm sorry, this map is empty!");

Thanks so much to everyone and sorry for my English e for my Java :-) You reply me so fast, also thanks really to everyone. 非常感谢大家,并为我的Java Java英语e::)回复速度如此之快,也非常感谢大家。

I wrote this code, that is ok: 我写了这段代码,没关系:

if (disponibilita.isEmpty())
{
   System.out.println("Sorry, ..");
}
else 
   System.out.println(disponibilita);

If it's null , then it isn't even a map; 如果为null ,那么它甚至都不是地图; if it's a map, only then it gets the chance of being empty. 如果是地图,则只有它有空的机会。

An empty map's toString will commonly return {} , whereas String.valueOf(null) returns the string "null" , and this is what is printed. 空映射的toString通常会返回{} ,而String.valueOf(null)返回字符串"null" ,这就是打印出来的内容。

If you want to print "sorry, this map is empty" when you have a map, but it is empty, then you need 如果要打印“对不起,这张地图是空的”,但它是空的,则需要

if (map.isEmpty()) System.out.println("sorry, this map is empty");

As said in the docs : 文档中所述:

The string representation consists of a list of key-value mappings in the order returned by the map's entrySet view's iterator, enclosed in braces ("{}"). 字符串表示形式由键值映射列表组成,这些键值映射由映射的entrySet视图的迭代器返回,并用大括号(“ {}”)括起来。 Adjacent mappings are separated by the characters ", " (comma and space). 相邻的映射用字符“,”(逗号和空格)分隔。 Each key-value mapping is rendered as the key followed by an equals sign ("=") followed by the associated value. 每个键值映射均表示为键,后跟等号(“ =”),后跟关联值。

That means the map is empty, you can test by calling isEmpty() . 这意味着映射为空,您可以通过调用isEmpty()进行测试。 If it were null, it would simply print out null . 如果为null,则只需打印出null

You mean, like this? 你的意思是这样吗?

if (nameOfanHashMap == null) {
    System.out.println("I'm sorry, this map is null!");
} else if (nameOfanHashMap.isEmpty()) {
    System.out.println("I'm sorry, this map is empty!");
} else {
    System.out.println(nameOfanHashMap);
} 

Notice that a Map that gets printed as {} is empty , but non- null . 请注意,以{}打印的Map ,但非null A null map will get printed as null . null图将被打印为null

If a map is null it is printed as null just like any other object. 如果地图为null ,则与其他任何对象一样,其被打印为null

If it is any but null it must be something, and { } means it is empty. 如果它是除null任何null则必须是某种东西,并且{ }表示它为空。

When you pass an Object to System.out.println, its method toString is called. 将对象传递给System.out.println时,将调用其方法toString。

Now, if the map has been instantiated but it is empty, the 现在,如果地图已实例化但为空,则

toString()

method returns 方法返回

{} {}

otherwise if it is null, the toString returns 否则,如果为null,则toString返回

null 空值

Finally, the check can be done with: 最后,可以使用以下方法进行检查:

if (nameOfanHashMap == null) {
    System.out.println("I'm sorry, this map is null!");
} else if (nameOfanHashMap.isEmpty()) {
    System.out.println("I'm sorry, this map is empty!");
} else {
    System.out.println(nameOfanHashMap);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM