Hashmap的存值:
1 public static void main(String[] args) {
2 ///*Integer*/map.put("1", 1);//向map中添加值(返回这个key以前的值,如果没有返回null)
3 HashMap<String, Integer> map=new HashMap<>();
4 System.out.println(map.put("1", 1));//null
5 System.out.println(map.put("1", 2));//1
6 }
Hashmap的取值:
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 map.put("DEMO", 1);
4 /*Value的类型*///得到map中key相对应的value的值
5 System.out.println(map.get("1"));//null
6 System.out.println(map.get("DEMO"));//1
7 }
Hashmap的判断为空:
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 /*boolean*///判断map是否为空
4 System.out.println(map.isEmpty());//true
5 map.put("DEMO", 1);
6 System.out.println(map.isEmpty());//false
7 }
Hashmap判断是否含有key:
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 /*boolean*///判断map中是否存在这个key
4 System.out.println(map.containsKey("DEMO"));//false
5 map.put("DEMO", 1);
6 System.out.println(map.containsKey("DEMO"));//true
7 }
Hashmap判断是否含有value:
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 /*boolean*///判断map中是否存在这个value
4 System.out.println(map.containsValue(1));//false
5 map.put("DEMO", 1);
6 System.out.println(map.containsValue(1));//true
7 }
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显示所有的value值:
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 /*Collection<Integer>*///显示所有的value值
4 System.out.println(map.values());//[]
5 map.put("DEMO1", 1);
6 map.put("DEMO2", 2);
7 System.out.println(map.values());//[1, 2]
8 }
Hashmap的元素个数:
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 /*int*///显示map里的值得数量
4 System.out.println(map.size());//0
5 map.put("DEMO1", 1);
6 System.out.println(map.size());//1
7 map.put("DEMO2", 2);
8 System.out.println(map.size());//2
9 }
Hashmap删除这个key值下的value:
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 /*SET<String>*///显示map所有的key
4 System.out.println(map.keySet());//[]
5 map.put("DEMO1", 1);
6 System.out.println(map.keySet());//[DEMO1]
7 map.put("DEMO2", 2);
8 System.out.println(map.keySet());//[DEMO1, DEMO2]
9 }
Hashmap删除这个key和value:
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 /*boolean*///删除这个键值对
4 map.put("DEMO1", 1);
5 map.put("DEMO2", 2);
6 System.out.println(map);//{DEMO1=1, DEMO2=2}
7 System.out.println(map.remove("DEMO2", 1));//false
8 System.out.println(map.remove("DEMO2", 2));//true
9 System.out.println(map);//{DEMO1=1}
10 }
Hashmap显示所有的key和value:
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 /*SET<map<String,Integer>>*///显示所有的key和value
4 System.out.println(map.entrySet());//[]
5 map.put("DEMO1", 1);
6 System.out.println(map.entrySet());//[DEMO1=1]
7 map.put("DEMO2", 2);
8 System.out.println(map.entrySet());//[DEMO1=1, DEMO2=2]
9 }
Hashmap添加另一个同一类型的map下的所有制:
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 HashMap<String, Integer> map1=new HashMap<>();
4 /*void*///将同一类型的map添加到另一个map中
5 map1.put("DEMO1", 1);
6 map.put("DEMO2", 2);
7 System.out.println(map);//{DEMO2=2}
8 map.putAll(map1);
9 System.out.println(map);//{DEMO1=1, DEMO2=2}
10 }
Hashmap替换这个key的value:(java8)
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 /*value*///判断map中是否存在这个key
4 map.put("DEMO1", 1);
5 map.put("DEMO2", 2);
6 System.out.println(map);//{DEMO1=1, DEMO2=2}
7 System.out.println(map.replace("DEMO2", 1));//2
8 System.out.println(map);//{DEMO1=1, DEMO2=1}
9 }
清空这个hashmap:
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 /*void*///清空map
4 map.put("DEMO1", 1);
5 map.put("DEMO2", 2);
6 System.out.println(map);//{DEMO1=1, DEMO2=2}
7 map.clear();//2
8 System.out.println(map);//{}
9 }
Hashmap的克隆:
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 /*object*///克隆这个map
4 map.put("DEMO1", 1);
5 map.put("DEMO2", 2);
6 System.out.println(map.clone());//{DEMO1=1, DEMO2=2}
7 Object clone = map.clone();
8 System.out.println(clone);//{DEMO1=1, DEMO2=2}
9 }
如果当前 Map 不存在键 key 或者该 key 关联的值为 null,那么就执行 put(key, value);否则,便不执行 put 操作:(java8新增方法)
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}
}
如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)compute 方法更适用于更新 key 关联的 value 时,新值依赖于旧值的情况
1 public static void main(String[] args) {
2 HashMap<String, Integer> map=new HashMap<>();
3 /*boolean*///当这个value为null时为1,否则为3
4 map.put("DEMO1", 1);
5 map.put("DEMO2", 2);
6 System.out.println(map);//{DEMO1=1, DEMO2=2}
7 map.compute("DEMO2", (k,v)->v==null?1:3);
8 System.out.println(map);//{DEMO1=1, DEMO2=3}
9 }
原文作者:九号云
链接:https://www.cnblogs.com/jiuhaoyun/p/8985643.html