···
public class MapDemo2 {
/*entrySet方法,键值对映射关系(结婚证)获取
* 实现步骤:
* 1.调用map集合方法entrySet()将集合中的映射关系对象,存储到set集合Set<Entry<k,v>>
* 2.迭代Set集合
* 3.获取出的Set集合的元素,是映射关系对象
* 4.通过映射关系对象方法getKet,getValue获取键值对
* */
public static void main(String[] args) {
function();
}
public static void function() {
Map<String, Integer> map=new HashMap<String,Integer>();
map.put("a", 11);
map.put("b", 12);
map.put("c", 13);
map.put("d", 41);
map.put("e", 15);
Set<Map.Entry<String,Integer>> entry=map.entrySet();//Entry是个内部类,用Map.Entry
Iterator<Map.Entry<String, Integer>> iterator=entry.iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> entry1=iterator.next();
String key=entry1.getKey();
Integer value=entry1.getValue();
System.out.println(key+"..."+value);
}
}
public static void function1() {//增强for循环的Entry
Map<String, Integer> map=new HashMap<String,Integer>();
map.put("a", 11);
map.put("b", 12);
map.put("c", 13);
map.put("d", 41);
map.put("e", 15);
for(Map.Entry<String,Integer> entry:map.entrySet()) {
String key=entry.getKey();
Integer value=entry.getValue();
System.out.println(key+"..."+value);
}
}
}