Java使用entrySet方法获取Map集合中的元素

时间:2021-05-20

本文为大家分享了使用entrySet方法获取Map集合中元素的具体代码,供大家参考,具体内容如下

package pack04;import java.util.*;public class MapDemo { public static void main(String[] args) { Map<Integer, String> ma = new HashMap<Integer, String>(); //给集合中存入元素 ma.put(1, "abc01"); //这里将基本数据类型自动装箱 ma.put(2, "abc02"); ma.put(3, "abc03"); ma.put(4, "abc04"); //将Map集合当中的映射关系取出,存入到Set集合当中 Set< Map.Entry<Integer, String> > entryset = ma.entrySet(); //取迭代器 Iterator< Map.Entry<Integer, String> > it = entryset.iterator(); //获取Map集合中的元素 while( it.hasNext() ) { Map.Entry<Integer, String> en = it.next(); Integer maKey = en.getKey(); String maValue = en.getValue(); System.out.println( maKey + " : " + maValue ); } }}

注:希望与各位读者相互交流,共同学习进步。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章