mapent

 1 package test12;
 2 
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.Map;
 6 import java.util.Set;
 7 
 8 public class Entre_Demo {
 9     public static  void  main(String ... args){
10         mapEntre();
11 
12     }
13     public static void  mapEntre(){
14         Map<String,Integer>  map=new HashMap<>();
15         map.put("tom",22);
16         map.put("ok",12);
17         Set<Map.Entry<String,Integer>> mapen=map.entrySet();//泛型内嵌套泛型。获得是Set集合。
18         Iterator<Map.Entry<String,Integer>>  mapit=mapen.iterator();
19         while (mapit.hasNext()){
20             Map.Entry<String,Integer> mape=mapit.next();
21 //            String mapkey=mapit.next().getKey();
22             String mapkey=mape.getKey();//获取键值
23             Integer mapval=mape.getValue();//获得val。
24 //            Integer mapval=mapit.next().getValue();
25             System.out.print(mapkey+"-------------------"+mapval);
26         }
27     }
28 }

 Map的方法中entrySet()返回是Set集合,集合内的对象类型为:Map.Entry<k,v>类型,属于泛型中嵌套另一个类型。

Set<Map.Entry<K,V>>      entrySet()

Interface Map.Entry<K,V> 提供的方法。entrySet()是返回的是这个接口实现类的对象。

看下方法:

K   getKey()

V     getValue()

V     setValue(V value) 设置新的value值。

原文地址:https://www.cnblogs.com/evilliu/p/7867230.html