定义一个Map集合
Map<String,Integer> map=new HashMap<String,Integer>();
//map中key不同,value可以相同
map.put("a",1);
map.put("b",1);
map.put("c",1);
System.out.println(map);
//根据key来取value
System.out.println("获取b的value为:"+map.get("b"));
//根据key来移除value
map.remove("c");
System.out.println("移除c后的map集合为:"+map);
//获取集合的长度
System.out.println("map集合的长度为:"+map.size());
//判断当前map集合是否包含指定的key
System.out.println(map.containsKey("a"));
//判断当前map集合是否包含指定的Value
System.out.println(map.containsValue(1));
//获取map集合的key的集合
System.out.println(map.keySet());
//获取map集合的所有value值
System.out.println(map.values());
//遍历map集合,通过map.keySet();
Set<String> keys = map.keySet();
System.out.println(keys+"11111111111111111111");
for (String key: keys){
System.out.println("key:"+key+","+"value:"+map.get(key));
}
//通过map.entrySet();遍历map集合
Set<Map.Entry<String, Integer>> entries = map.entrySet();
for (Map.Entry<String,Integer> en:entries){
System.out.println("key:"+en.getKey()+","+"value:"+map.get(en.getValue()));
}