![](https://img-blog.csdnimg.cn/20210716161527635.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0Vsb25TdW4=,size_16,color_FFFFFF,t_70)
代码如下:
package com.learn.common;
import java.util.HashMap;
import java.util.Map;
public class mapExa {
public static Map<String,Object> getAmap(){
Map<String,Object>map=new HashMap<>();
map.put("a",1);
map.put("b",8);
map.put("c",5);
map.put("d",3);
map.put("e",9);
map.put("f",2);
map.put("g",11);
return map;
}
package com.learn.Map;
import com.learn.common.mapExa;
import java.util.Iterator;
import java.util.Map;
public class mapOperation {
public static void main(String[] args) {
Map<String,Object> map= mapExa.getAmap();
//遍历方法1
System.out.println("遍历方法1结果");
Map1Traversal(map);
//遍历方法2
System.out.println("遍历方法2结果");
Map2Traversal(map);
//遍历方法3
System.out.println("遍历方法3结果");
Map3Traversal(map);
//遍历方法4
System.out.println("遍历方法4结果");
Map4Traversal(map);
}
//关于将map遍历方法1 //注意foreach中不能检查空对象 否则会报错
public static void Map1Traversal(Map<String,Object>map){
for (Map.Entry<String,Object> entry:map.entrySet()) {
System.out.println("entry --> key:" +entry.getKey()+" value:" +entry.getValue());
}
}
//关于将map遍历方法2 适用于只拿到 key或者只拿到value
public static void Map2Traversal(Map<String,Object>map){
for (String key:map.keySet()) {
System.out.println("entry --> key:" +key);
}
for (Object value:map.values()) {
System.out.println("entry --> value:" +value);
}
}
//关于遍历方法3 使用Iterable 遍历器
public static void Map3Traversal(Map<String,Object>map) {
Iterator<Map.Entry<String,Object>> iterator= (Iterator<Map.Entry<String, Object>>) map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String,Object> entry=iterator.next();
System.out.println("entry --> key:"+entry.getKey());
System.out.println("entry --> value:"+entry.getValue());
}
}
//遍历方法4
public static void Map4Traversal(Map<String,Object>map) {
for (String key:map.keySet()){
System.out.println("entry --> key:"+key);
System.out.println("entry --> value:"+map.get(key));
}
}
}
运行结果:
遍历方法1结果
entry --> key:a value:1
entry --> key:b value:8
entry --> key:c value:5
entry --> key:d value:3
entry --> key:e value:9
entry --> key:f value:2
entry --> key:g value:11
遍历方法2结果
entry --> key:a
entry --> key:b
entry --> key:c
entry --> key:d
entry --> key:e
entry --> key:f
entry --> key:g
entry --> value:1
entry --> value:8
entry --> value:5
entry --> value:3
entry --> value:9
entry --> value:2
entry --> value:11
遍历方法3结果
entry --> key:a
entry --> value:1
entry --> key:b
entry --> value:8
entry --> key:c
entry --> value:5
entry --> key:d
entry --> value:3
entry --> key:e
entry --> value:9
entry --> key:f
entry --> value:2
entry --> key:g
entry --> value:11
遍历方法4结果
entry --> key:a
entry --> value:1
entry --> key:b
entry --> value:8
entry --> key:c
entry --> value:5
entry --> key:d
entry --> value:3
entry --> key:e
entry --> value:9
entry --> key:f
entry --> value:2
entry --> key:g
entry --> value:11
2.Map的compute方法:
作用 对特定key的键值对进行处理。
示例代码,比哪里Map,打印所有值大于4的键值对并求和。
package com.learn.common;
import java.util.HashMap;
import java.util.Map;
public class mapExa {
public static Map<String,Object> getAmap(){
Map<String,Object>map=new HashMap<>();
map.put("a",1);
map.put("b",8);
map.put("c",5);
map.put("d",3);
map.put("e",9);
map.put("f",2);
map.put("g",11);
return map;
}
public static void MapCompute(Map<String,Object>map) {
int result=0;
System.out.println("大于4的值:");
for (String key1:map.keySet()){
result+=Integer.parseInt(
map.compute(key1,(key,value)->{
if (Integer.parseInt(value.toString())>4){
System.out.println("key : "+key+" "+" value: "+value);
}
return value;
}).toString()
);
}
System.out.println("和:"+result);
}
结果:
2.Map的判空方法:
1.使用Map的isEmpty()方法:
源码:
public boolean isEmpty() {
return size() == 0;
}
可以看到,使用isEmpty方法来判空时,必须是实例化的Map类型,也就是说如果map为null的话就会报空指针错误。
3.Map的put()和putIfAbsent():
如名称所示一个是不管之前有没有这个key就直接放进去,如果之前有的话则会给覆盖掉。一个是如果没有这个Key,则放进去。