Java中Map里面put方法的返回值:

  1. 如果key不存在,返回值是null;
  2. key存在, 返回原先被替换的value值

map中的key,value都可以是null

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Map map = new HashMap();
Object obj = null;
obj = map.put(null, null);
System.out.println(obj); //null
obj = map.put(null, "123"); 
System.out.println(obj); //null
obj = map.put(null, null);
System.out.println(obj); //123

obj = map.put(1, 123);
System.out.println(obj); //null
obj = map.put(1, "123");
System.out.println(obj); //123
obj = map.put(1, 123);
System.out.println(obj); //123