在Map集合中,值对象为null且没有数量限制,因此get ) )方法的返回值为null,如果集合中没有关键对象,或者关键对象没有映射值对象,即值对象因此,在Map集合中,应该用containsKey ) )方法来判断是否存在某个键,而不是用get ()方法来判断。
此方法确定Map集合对象是否包含指定的键名。 如果Map集合包含指定的键名,则返回true;否则返回false。
语法内容密钥(对象密钥)。
publicstaticvoidmain (字符串[ ] args ) {
Map map=new HashMap (; 定义//map对象
map.put (苹果,)新鲜苹果); //将对象添加到集合中
map.put('computer ','配置良好的计算机');
map.put(book,)堆积成山的书);
map.put('time ',new Date ) );
String key='book ';
boolean contains=map.contains key (key; //判断是否包含指定的键值
if (如果contains (//条件为真)
在system.out.println('map集合中包含键名' key ); //输出信息
} else {
system.out.println('map集合中不包含键名' key );
}
}