boolean containsKey(Object key) 方法:判断集合中是否完全包含与key相同的键。
返回结果:包含返回true 反之 false
实例:
@Test
public void test() {
Map<String,String> map = new HashMap<String, String>();
//添加数据
map.put("1001", "张三");
map.put("1002", "李四");
map.put("1003", "李四");
//输出结果
System.out.println("该Map集合中是否包含1001键名:"+(map.containsKey("1001")?"包含":"不包含"));
System.out.println("该Map集合中是否包含100键名:"+(map.containsKey("100")?"包含":"不包含"));
}
console :
该Map集合中是否包含1001键名:包含
该Map集合中是否包含100键名:不包含
boolean containsValue(Object value) 方法:判断集合中是否有一个或者多个与value相同的值。
返回结果:包含true 反之 false
实例:
@Test
public void test() {
Map<String,String> map = new HashMap<String, String>();
//添加数据
map.put("1001", "张三");
map.put("1002", "李四");
map.put("1003", "李四");
//输出结果
System.out.println("该Map集合中是否包含学生 李四 :"+(map.containsValue("李四")?"包含":"不包含"));
System.out.println("该Map集合中是否包含学生 王五:"+(map.containsValue("王五")?"包含":"不包含"));
}
console:
该Map集合中是否包含学生 李四 :包含
该Map集合中是否包含学生 王五:不包含