Java中Map的entrySet()详解

转发:原博客

由于Map中存放的元素均为键值对,故每一个键值对必然存在一个映射关系。
Map中采用Entry内部类来表示一个映射项,映射项包含Key和Value
Map.Entry里面包含getKey()和getValue()方法

 

Set<Entry<T,V>> entrySet() :

该方法返回值就是这个map中各个键值对映射关系的集合,此集合的类型为Map.Entry。

 

可使用它对map进行遍历。

1 Iterator<Map.Entry<Integer, Integer>> it=map.entrySet().iterator();
2 while(it.hasNext()) {
3 Map.Entry<Integer,Integer> entry=it.next();
4 int key=entry.getKey();
5 int value=entry.getValue();
6 System.out.println(key+" "+value);
7 }

 



posted on 2020-07-06 13:33  CHENSISI  阅读(1986)  评论(0编辑  收藏  举报