hashMap放入值
public static void main(String[] args) {
///*Integer*/map.put("1", 1);//向map中添加值(返回这个key以前的值,如果没有返回null)
HashMap<String, Integer> map=new HashMap<>();
System.out.println(map.put("1", 1));//null
System.out.println(map.put("1", 2));//1
}
hashMap取值
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
map.put("DEMO", 1);
/*Value的类型*///得到map中key相对应的value的值
System.out.println(map.get("1"));//null
System.out.println(map.get("DEMO"));//1
}
hashMap判断是否为空
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*boolean*///判断map是否为空
System.out.println(map.isEmpty());//true
map.put("DEMO", 1);
System.out.println(map.isEmpty());//false
}
hashMap判断是否有key
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*boolean*///判断map中是否存在这个key
System.out.println(map.containsKey("DEMO"));//false
map.put("DEMO", 1);
System.out.println(map.containsKey("DEMO"));//true
}
hashMap判断是否有value