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循環(huán)
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); }
以上就是對(duì)Java 遍歷MAP的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
相關(guān)文章
Java spring事務(wù)及事務(wù)不生效的原因詳解
在日常編碼過程中常常涉及到事務(wù),在前兩天看到一篇文章提到了Spring事務(wù),那么在此總結(jié)下在Spring環(huán)境下事務(wù)失效的幾種原因2021-09-09Java 判斷字符串a(chǎn)和b是否互為旋轉(zhuǎn)詞
本篇文章主要介紹了判斷字符串a(chǎn)和b是否互為旋轉(zhuǎn)詞的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-05-05使用dom4j遞歸解析節(jié)點(diǎn)內(nèi)還含有多個(gè)節(jié)點(diǎn)的xml
這篇文章主要介紹了使用dom4j遞歸解析節(jié)點(diǎn)內(nèi)還含有多個(gè)節(jié)點(diǎn)的xml,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09java對(duì)象轉(zhuǎn)化成String類型的四種方法小結(jié)
在java項(xiàng)目的實(shí)際開發(fā)和應(yīng)用中,常常需要用到將對(duì)象轉(zhuǎn)為String這一基本功能。本文就詳細(xì)的介紹幾種方法,感興趣的可以了解一下2021-08-08idea2020導(dǎo)入spring5.1的源碼詳細(xì)教程
這篇文章主要介紹了idea2020導(dǎo)入spring5.1的源碼的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06