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
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*boolean*///判断map中是否存在这个value
System.out.println(map.containsValue(1));//false
map.put("DEMO", 1);
System.out.println(map.containsValue(1));//true
}
hashMap删除指定key下的value
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*Integer*///删除key值下的value
System.out.println(map.remove("1"));//null
map.put("DEMO", 2);
System.out.println(map.remove("DEMO"));//2(删除的值)
}
hashMap显示所有的key
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*SET<String>*///显示map所有的key
System.out.println(map.keySet());//[]
map.put("DEMO1", 1);
System.out.println(map.keySet());//[DEMO1]
map.put("DEMO2", 2);
System.out.println(map.keySet());//[DEMO1, DEMO2]
}
hashMap显示所有的value
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*Collection<Integer>*///显示所有的value值
System.out.println(map.values());//[]
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map.values());//[1, 2]
}
hashMap的元素个数
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*int*///显示map里的值得数量
System.out.println(map.size());//0
map.put("DEMO1", 1);
System.out.println(map.size());//1
map.put("DEMO2", 2);
System.out.println(map.size());//2
}
hashMap显示所有的key和value
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*SET<map<String,Integer>>*///显示所有的key和value
System.out.println(map.entrySet());//[]
map.put("DEMO1", 1);
System.out.println(map.entrySet());//[DEMO1=1]
map.put("DEMO2", 2);
System.out.println(map.entrySet());//[DEMO1=1, DEMO2=2]
}
hashMap添加一个map到另一个同类型的map下
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
HashMap<String, Integer> map1=new HashMap<>();
/*void*///将同一类型的map添加到另一个map中
map1.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);//{DEMO2=2}
map.putAll(map1);
System.out.println(map);//{DEMO1=1, DEMO2=2}
}
hashMap删除指定的key和value(key和value都作为删除条件,两者需要同时成立)
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*boolean*///删除这个键值对
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);//{DEMO1=1, DEMO2=2}
System.out.println(map.remove("DEMO2", 1));//false
System.out.println(map.remove("DEMO2", 2));//true
System.out.println(map);//{DEMO1=1}
}
hashMap替换某个key的value
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*value*///判断map中是否存在这个key
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);//{DEMO1=1, DEMO2=2}
System.out.println(map.replace("DEMO2", 1));//2
System.out.println(map);//{DEMO1=1, DEMO2=1}
}
清空hashMap
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*void*///清空map
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);//{DEMO1=1, DEMO2=2}
map.clear();//2
System.out.println(map);//{}
}
克隆hashMap
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*object*///克隆这个map
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map.clone());//{DEMO1=1, DEMO2=2}
Object clone = map.clone();
System.out.println(clone);//{DEMO1=1, DEMO2=2}
}
hashMap中如果键key不存在或者关联的值为null,那么就执行put(key,value),否则不执行!
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*boolean*///判断map中是否存在这个key
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);//{DEMO1=1, DEMO2=2}
System.out.println(map.putIfAbsent("DEMO1", 12222));//1
System.out.println(map.putIfAbsent("DEMO3", 12222));//null
System.out.println(map);//{DEMO1=1, DEMO2=2,DEMO3=12222}
}
hahsMap的compute方法适用于更新key关联的value时,新值依赖于旧值的情况
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*boolean*///当这个value为null时为1,否则为3
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);//{DEMO1=1, DEMO2=2}
map.compute("DEMO2", (k,v)->v==null?1:3);
System.out.println(map);//{DEMO1=1, DEMO2=3}
}