java 遍歷Map及Map轉(zhuǎn)化為二維數(shù)組的實(shí)例
更新時(shí)間:2017年08月21日 09:25:52 作者:chs0113
這篇文章主要介紹了java 遍歷Map及Map轉(zhuǎn)化為二維數(shù)組的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
java 遍歷Map及Map轉(zhuǎn)化為二維數(shù)組的實(shí)例
實(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ù)組的長(zhǎng)度為:" + 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ù)組的長(zhǎng)度為:" + 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ù)組的長(zhǎng)度為:" + 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ù)組的長(zhǎng)度為: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ù)組的長(zhǎng)度為: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ù)組的長(zhǎng)度為:4 ---------------------------------------------------- key = 112013012014 and value = 李四 key = 112013012013 and value = 李三 key = 012013012013 and value = 張三 key = 012013012014 and value = 張四
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,大家共同進(jìn)步,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
您可能感興趣的文章:
- java數(shù)組遍歷 刪除remove(示例代碼)
- Java中使用While語句自增運(yùn)算遍歷數(shù)組典型實(shí)例
- Java中遍歷數(shù)組使用foreach循環(huán)還是for循環(huán)?
- Java數(shù)組的遍歷與求和知識(shí)點(diǎn)
- java二維數(shù)組遍歷的2種代碼
- Java數(shù)組常見應(yīng)用詳解【創(chuàng)建、遍歷、排序、查找】
- java8新特性 stream流的方式遍歷集合和數(shù)組操作
- 劍指Offer之Java算法習(xí)題精講N叉樹的遍歷及數(shù)組與字符串
- Java技巧函數(shù)方法實(shí)現(xiàn)二維數(shù)組遍歷
相關(guān)文章
教你1秒將本地SpringBoot項(xiàng)目jar包部署到Linux環(huán)境(超詳細(xì)!)
spring Boot簡(jiǎn)化了Spring應(yīng)用的開發(fā)過程,遵循約定優(yōu)先配置的原則提供了各類開箱即用(out-of-the-box)的框架配置,下面這篇文章主要給大家介紹了關(guān)于1秒將本地SpringBoot項(xiàng)目jar包部署到Linux環(huán)境的相關(guān)資料,超級(jí)詳細(xì),需要的朋友可以參考下2023-04-04
Java微信掃碼登錄功能并實(shí)現(xiàn)認(rèn)證授權(quán)全過程
這篇文章主要給大家介紹了關(guān)于Java微信掃碼登錄功能并實(shí)現(xiàn)認(rèn)證授權(quán)的相關(guān)資料,要在Java中實(shí)現(xiàn)微信掃碼登錄,您可以按照以下步驟進(jìn)行操作,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
Java 內(nèi)置Http Server構(gòu)建web應(yīng)用案例詳解
這篇文章主要介紹了Java 內(nèi)置Http Server構(gòu)建web應(yīng)用案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Eclipse可視化插件WindowBuilder的安裝方法
這篇文章主要介紹了Eclipse可視化插件WindowBuilder的安裝方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Java批量操作文件系統(tǒng)的實(shí)現(xiàn)示例
文件上傳和下載是java web中常見的操作,本文主要介紹了Java批量操作文件系統(tǒng)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03

