java 遍历MAP java 遍历MAP的几种方法代码实例
rain_2372 人气:0想了解java 遍历MAP的几种方法代码实例的相关内容吗,rain_2372在本文为您仔细讲解java 遍历MAP的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,遍历MAP方法,java,MAP,Java,MAP遍历,下面大家一起来学习吧。
java中遍历MAP的几种方法
Map<String,String> map=new HashMap<String,String>(); map.put("username", "qq"); map.put("passWord", "123"); map.put("userID", "1"); map.put("email", "qq@qq.com"); Map<String,String> map=new HashMap<String,String>(); map.put("username", "qq"); map.put("passWord", "123"); map.put("userID", "1"); map.put("email", "qq@qq.com");
第一种用for循环
for(Map.Entry<String, String> entry:map.entrySet()){ System.out.println(entry.getKey()+"--->"+entry.getValue()); } for(Map.Entry<String, String> entry:map.entrySet()){ System.out.println(entry.getKey()+"--->"+entry.getValue()); }
第二种用迭代
Set set = map.entrySet(); Iterator i = set.iterator(); while(i.hasNext()){ Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next(); System.out.println(entry1.getKey()+"=="+entry1.getValue()); } Set set = map.entrySet(); Iterator i = set.iterator(); while(i.hasNext()){ Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next(); System.out.println(entry1.getKey()+"=="+entry1.getValue()); }
用keySet()迭代
Iterator it=map.keySet().iterator(); while(it.hasNext()){ String key; String value; key=it.next().toString(); value=map.get(key); System.out.println(key+"--"+value); } Iterator it=map.keySet().iterator(); while(it.hasNext()){ String key; String value; key=it.next().toString(); value=map.get(key); System.out.println(key+"--"+value); }
用entrySet()迭代
Iterator it=map.entrySet().iterator(); System.out.println( map.entrySet().size()); String key; String value; while(it.hasNext()){ Map.Entry entry = (Map.Entry)it.next(); key=entry.getKey().toString(); value=entry.getValue().toString(); System.out.println(key+"===="+value); }
以上就是对Java 遍历MAP的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!
加载全部内容