JAva遍歴MapおよびMapから二次元配列への変換例

4955 ワード

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 map1 = new HashMap(); 
    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 map2 = new HashMap(); 
    map2.put("112013012013", "  "); 
    map2.put("112013012014", "  "); 
    System.out.println("
" + " : Map.entrySet() iterator() map2 key value"); Iterator> iterator = map2.entrySet().iterator(); String[][] group2 = new String[map2.size()][2]; while (iterator.hasNext()) { Map.Entry 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 map = new HashMap(); map.putAll(map1); map.putAll(map2); String[][] group3 = new String[map.size()][2]; System.out.println("
" + " : Map.entrySet() map key value "); for (Map.Entry 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 =    

もし疑問があれば伝言を残してあるいは当駅のコミュニティに行って討論を交流して、みんなは共に進歩して、読書に感謝して、みんなを助けることができることを望んで、みんなの当駅に対する支持に感謝します!