java 遍歷Map及Map轉(zhuǎn)化為二維數(shù)組的實例
更新時間:2017年08月21日 09:25:52 作者:chs0113
這篇文章主要介紹了java 遍歷Map及Map轉(zhuǎn)化為二維數(shù)組的實例的相關(guān)資料,希望通過本文能幫助到大家,實現(xiàn)這樣的功能,需要的朋友可以參考下
java 遍歷Map及Map轉(zhuǎn)化為二維數(shù)組的實例
實例代碼:
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Test { public static void main(String[] args) { int a = 0, b = 0, c = 0; // 第一種:通過Map.keySet()遍歷Map及將Map轉(zhuǎn)化為二維數(shù)組 Map<String, String> map1 = new HashMap<String, String>(); map1.put("012013012013", "張三"); map1.put("012013012014", "張四"); String[][] group1 = new String[map1.size()][2]; System.out.println("第一種:通過Map.keySet()遍歷map1的key和value"); for (String key : map1.keySet()) { System.out.println("key = " + key + " and value = " + map1.get(key)); group1[a][0] = key; group1[a][1] = map1.get(key); a++; } System.out.println("map1.size()為:" + map1.size() + ",a為:" + a + ",group1數(shù)組的長度為:" + group1.length); System.out.println("----------------------------------------------------"); for(int n = 0; n < group1.length; n++) { System.out.println("key = " + group1[n][0] + " and value = " + group1[n][1]); } // 第二種:通過Map.entrySet()使用iterator()遍歷Map及將Map轉(zhuǎn)化為二維數(shù)組 Map<String, String> map2 = new HashMap<String, String>(); map2.put("112013012013", "李三"); map2.put("112013012014", "李四"); System.out.println("\n" + "第二種:通過Map.entrySet()使用iterator()遍歷map2的key和value"); Iterator<Map.Entry<String, String>> iterator = map2.entrySet().iterator(); String[][] group2 = new String[map2.size()][2]; while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue()); group2[b][0] = entry.getKey(); group2[b][1] = entry.getValue(); b++; } System.out.println("map2.size()為:" + map2.size() + ",b為:" + b + ",group2數(shù)組的長度為:" + group2.length); System.out.println("----------------------------------------------------"); for(int n = 0; n < group2.length; n++) { System.out.println("key = " + group2[n][0] + " and value = " + group2[n][1]); } // 第三種:通過Map.entrySet()遍歷遍歷Map及將Map轉(zhuǎn)化為二維數(shù)組 Map<String, String> map = new HashMap<String, String>(); map.putAll(map1); map.putAll(map2); String[][] group3 = new String[map.size()][2]; System.out.println("\n" + "第三種:通過Map.entrySet()遍歷map的key和value "); for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue()); group3[c][0] = entry.getKey(); group3[c][1] = entry.getValue(); c++; } System.out.println("map.size()為:" + map.size() + ",c為:" + c + ",group3數(shù)組的長度為:" + group3.length); System.out.println("----------------------------------------------------"); for(int n = 0; n < group3.length; n++) { System.out.println("key = " + group3[n][0] + " and value = " + group3[n][1]); } } }
輸出結(jié)果為:
第一種:通過Map.keySet()遍歷map1的key和value key = 012013012013 and value = 張三 key = 012013012014 and value = 張四 map1.size()為:2,a為:2,group1數(shù)組的長度為:2 ---------------------------------------------------- key = 012013012013 and value = 張三 key = 012013012014 and value = 張四 第二種:通過Map.entrySet()使用iterator()遍歷map2的key和value key = 112013012014 and value = 李四 key = 112013012013 and value = 李三 map2.size()為:2,b為:2,group2數(shù)組的長度為:2 ---------------------------------------------------- key = 112013012014 and value = 李四 key = 112013012013 and value = 李三 第三種:通過Map.entrySet()遍歷map的key和value key = 112013012014 and value = 李四 key = 112013012013 and value = 李三 key = 012013012013 and value = 張三 key = 012013012014 and value = 張四 map.size()為:4,c為:4,group3數(shù)組的長度為:4 ---------------------------------------------------- key = 112013012014 and value = 李四 key = 112013012013 and value = 李三 key = 012013012013 and value = 張三 key = 012013012014 and value = 張四
如有疑問請留言或者到本站社區(qū)交流討論,大家共同進步,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
相關(guān)文章
Mybatis攔截器實現(xiàn)數(shù)據(jù)權(quán)限的示例代碼
在我們?nèi)粘i_發(fā)過程中,通常會涉及到數(shù)據(jù)權(quán)限問題,本文主要介紹了Mybatis攔截器實現(xiàn)數(shù)據(jù)權(quán)限的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03Springboot攔截器如何獲取@RequestBody參數(shù)
這篇文章主要介紹了Springboot攔截器如何獲取@RequestBody參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Java設(shè)計模式之享元模式(Flyweight Pattern)詳解
享元模式(Flyweight Pattern)是一種結(jié)構(gòu)型設(shè)計模式,旨在減少對象的數(shù)量,以節(jié)省內(nèi)存空間和提高性能,本文將詳細的給大家介紹一下Java享元模式,需要的朋友可以參考下2023-07-07解決IDEA啟動springboot項目報錯java.lang.ClassNotFoundException:?jav
這篇文章主要介紹了解決IDEA啟動springboot項目報錯java.lang.ClassNotFoundException:?javax.servlet.ServletContext問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01