服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java 遍历Map及Map转化为二维数组的实例

java 遍历Map及Map转化为二维数组的实例

2020-12-19 13:50chs0113 Java教程

这篇文章主要介绍了java 遍历Map及Map转化为二维数组的实例的相关资料,希望通过本文能帮助到大家,实现这样的功能,需要的朋友可以参考下

java 遍历Map及Map转化为二维数组的实例

实例代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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转化为二维数组
    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数组的长度为:" + 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转化为二维数组
    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数组的长度为:" + 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转化为二维数组
    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数组的长度为:" + 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]); 
    }
     
  }
}

输出结果为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
第一种:通过Map.keySet()遍历map1的key和value
key = 012013012013 and value = 张三
key = 012013012014 and value = 张四
map1.size()为:2,a为:2,group1数组的长度为: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数组的长度为: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数组的长度为:4
----------------------------------------------------
key = 112013012014 and value = 李四
key = 112013012013 and value = 李三
key = 012013012013 and value = 张三
key = 012013012014 and value = 张四

如有疑问请留言或者到本站社区交流讨论,大家共同进步,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/caihuangshi/article/details/17552933

延伸 · 阅读

精彩推荐