当我调用 map.get("key") 并且在 HashMap 中没有带有键“key”的条目时HashMap<String,String>
返回什么?
It returns null
. It's written in the documentation .
Returns: the value to which the specified key is mapped, or null if this map contains no mapping for the key
The first thing to do when you have such a specific question is to consult the documentation. Java APIs are documented reasonably well and tell you what is returned, what exceptions are thrown and what each argument means.
You can:
Check in your IDE
Map<String, String> map = new HashMap<String, String>();
map.put("foo", "fooValue");
System.out.println(map.get("bar")); // null
Check documentation - HashMap get() method description:
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
Careful - If you initialize it using
Map.of(key, val, key, val)
and then do a
get('key-that-isnt-there')
then you'll get a null pointer exception.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.