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", "[email protected]"); Map<String,String> map=new HashMap<String,String>(); map.put("username", "qq"); map.put("passWord", "123"); map.put("userID", "1"); map.put("email", "[email protected]");
第一種用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的材料整頓,後續持續彌補相干材料,感謝年夜家對本站的支撐!