转发:原博客

由于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 }

 



相关文章:

  • 2022-12-23
  • 2021-12-14
  • 2022-12-23
  • 2021-10-24
  • 2021-07-19
  • 2021-12-26
  • 2021-07-13
猜你喜欢
  • 2022-12-23
  • 2021-07-04
  • 2022-12-23
  • 2021-09-22
  • 2021-06-12
  • 2022-12-23
  • 2021-09-24
相关资源
相似解决方案