本文为大家分享了使用entrySet方法获取Map集合中元素的具体代码,供大家参考,具体内容如下
package pack04;
import java.util.*;
public class MapDemo {
public static void main(String[] args) {
Map ma = new HashMap();
//给集合中存入元素
ma.put(1, "abc01"); //这里将基本数据类型自动装箱
ma.put(2, "abc02");
ma.put(3, "abc03");
ma.put(4, "abc04");
//将Map集合当中的映射关系取出,存入到Set集合当中
Set< Map.Entry > entryset = ma.entrySet();
//取迭代器
Iterator< Map.Entry > it = entryset.iterator();
//获取Map集合中的元素
while( it.hasNext() ) {
Map.Entry en = it.next();
Integer maKey = en.getKey();
String maValue = en.getValue();
System.out.println( maKey + " : " + maValue );
}
}
}
注:希望与各位读者相互交流,共同学习进步。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



