entrySet

方法:

1.public K getKey() :获取Entry对象中的键。
2.public V getValue() :获取Entry对象中的值。
3.public Set<Map.Entry<K,V>> entrySet() : 获取到Map集合中所有的键值对对象的集合(Set集合)。

代码:

 1 import java.util.HashMap;
 2 import java.util.Map;
 3 import java.util.Set;
 4 
 5 public class EntryDemo {
 6     public static void main(String[] args) {
 7         //创建Map集和对象
 8         HashMap<String, String> hashMap = new HashMap<>();
 9         hashMap.put("马云","阿里巴巴");
10         hashMap.put("马化腾","腾讯");
11         hashMap.put("王健林","万达");
12         //获取所有的 Entry 对象
13         Set<Map.Entry<String, String>> entries = hashMap.entrySet();
14         //遍历得到每一个entry对象
15         for(Map.Entry<String,String> entry : entries){
16             System.out.println(entry.getKey()+" : "+entry.getValue());
17         }
18 
19 
20     }
21 }

猜你喜欢

转载自www.cnblogs.com/0error0warning/p/12528461.html