public class HashMapDemo {
public static void main(String[] args) {
//声明HashMap对象
Map<String, Integer> map = new HashMap<String, Integer>();
//添加数据
//map.put(key,value)
map.put("赵祯", 23) ;
map.put("老赵", 24) ;
map.put("老汉", 50) ;
map.put("老刘", 88) ;
map.put("Lisi",92);
//根据键值对键值获取数据
int value = map.get("赵祯") ;
System.out.println("数据值为:"+value) ;
//获取Map中键值对的个数
int size = map.size() ;
System.out.println("map中键值对个数为:"+size) ;
//判断Map集合中是否包含键为key的键值对
boolean b1 = map.containsKey("赵祯") ;
boolean b2 = map.containsKey("list") ;
System.out.println("是否包含键值为赵祯的键值对数据:" + b1) ;
System.out.println("是否包含键值为list的键值对数据:" + b2) ;
//判断Map集合中是否包含值为value的键值对
boolean b3=map.containsValue(23);
boolean b4=map.containsValue(25);
System.out.println("是否包含值为23的键值对数据:"+b3);
System.out.println("是否包含值为24的键值对数据:"+b4);
//判断Map集合中是否没有任何键值对【是否为空】
boolean b5=map.isEmpty();
System.out.println("map中键值对数据是否为空:"+b5);
//根据键值删除Map中键值对
int value2=map.remove("Lisi");
System.out.println("删除了键为Lisi的键值对数据,其值为:"+value2);
//清空Map集合中所有的键值对
map.clear();
boolean b7=map.isEmpty();
System.out.println("map中键值对数据是否为空:"+b7);
}
}
运行结果为:
坑疼死、恩题