欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-23 23:59:52
java 遍历map及map转化为二维数组的实例 实例代码: import java.util.hashmap; import java.util.iter...

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

实例代码:

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]);  
    } 
     
  } 
} 

输出结果为:

第一种:通过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 = 张四 

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