import java.util.Collection;标签:map,常用,java,HashMap,System,println,方法,out From: https://www.cnblogs.com/xhengge/p/17739019.html
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashMap09 {
public static void main(String[] args) {
HashMap<String,Integer> map = new HashMap<>();
//添加元素
map.put("a",1);
map.put("b",2);
map.put("c",3);
System.out.println(map);
//获取键值对的值
Integer value = map.get("a");
System.out.println(value);
//根据键删除键值对
map.remove("a");
System.out.println(map);
//判断是否包含某个键
if (map.containsKey("a")){
System.out.println("包含");
}else {
System.out.println("不包含");
}
//判断是否包含某个值
if (map.containsKey(3)){
System.out.println("包含");
}else {
System.out.println("不包含");
}
//获取键集合
Set<String> keys = map.keySet();
System.out.println(keys);
//获取值集合
Collection<Integer> values = map.values();
System.out.println(values);
//获取键值对集合
Set<Map.Entry<String, Integer>> entries = map.entrySet();
System.out.println(entries);
}
}