2012-04-05 5 views
0

2つのハッシュマップがあり、最初のハッシュマップの値となる3番目のハッシュマップを埋めたいと思います。 2番目のハッシュマップは配列に分割されます。 すなわち:キーと値を持つHas​​hMapは、Javaを使用して他の2つのハッシュマップの値と同じです

hashmap1 = {1=e1, 2=e2} 
hashmap2 = {10=word1-word2-word3, 20=word4-word5-word6} 
the result: 
hashmap3 = {e1=word1-word2-word3, e2=word4-word5-word6} 

これは私がこれまでにやったことです:

static HashMap<Integer, String> catnamecatkeys = new HashMap<Integer, String>(); 

    static HashMap<Integer, String> keywords = new HashMap<Integer, String>(); 

    static HashMap<String, String> tempHash = new HashMap<String, String>(); 

    static HashMap<String, String[]> hash = new HashMap<String, String[]>(); 

    static String[] arr; 

    public static void main(String[] args) { 

    catnamecatkeys.put(1, "e1"); 
    catnamecatkeys.put(2, "e2"); 
    keywords.put(1, "word1-word2-word3"); 
    keywords.put(2, "word4-word5-word6"); 

    for (int key : catnamecatkeys.keySet()) { 
     tempHash.put(catnamecatkeys.get(key),null); 
    } 

    for(String tempkey: tempHash.keySet()){   
     tempHash.put(tempkey,keywords.entrySet().iterator().next().getValue()); 
     arr = tempHash.get(tempkey).split("-"); 
     hash.put(tempkey, arr); 
    } 
    System.out.println(tempHash); 
    for (String hashkey : hash.keySet()) { 
     for (int i = 0; i < arr.length; i++) { 
      System.out.println(hashkey + ":" + hash.get(hashkey)[i]); 
     } 


     } 

    } 

が、出力は次のようになります。

hashmap3 = {e1=word1-word2-word3, e2=word1-word2-word3} 

任意のアイデアしてください?

+0

地図のアイテムを挿入順に関連付けるようにしていますか?一度HashMapクラスに挿入されると、反復の順序は挿入の順序と等しくなりません。 –

+0

マップ内のアイテムを挿入順(同じキーではなく)で関連付けると、イテレータの順序は挿入順に基づいていないため、HashMapは機能しません反復の順序は不特定である)。 –

答えて

1

あなたはここで、ループの外Iteratorを初期化する必要があることは完全な例である -

static HashMap<Integer, String> catnamecatkeys = new HashMap<Integer, String>(); 

static HashMap<Integer, String> keywords = new HashMap<Integer, String>(); 

static HashMap<String, String> tempHash = new HashMap<String, String>(); 

static HashMap<String, String[]> hash = new HashMap<String, String[]>(); 

static String[] arr; 
public static void main(String[] agrs) 
{  
    catnamecatkeys.put(1, "e1"); 
     catnamecatkeys.put(2, "e2"); 
     keywords.put(1, "word1-word2-word3"); 
     keywords.put(2, "word4-word5-word6"); 

     for (int key : catnamecatkeys.keySet()) { 
      tempHash.put(catnamecatkeys.get(key),null); 
     } 
    Set<Entry<Integer,String>> set = keywords.entrySet(); 
     Iterator<Entry<Integer, String>> iterator= set.iterator(); 
     for(String tempkey: tempHash.keySet()){   
      tempHash.put(tempkey,iterator.next().getValue()); 
      arr = tempHash.get(tempkey).split("-"); 
      hash.put(tempkey, arr); 
     } 
     System.out.println(tempHash); 
     for (String hashkey : hash.keySet()) { 
      for (int i = 0; i < arr.length; i++) { 
       System.out.println(hashkey + ":" + hash.get(hashkey)[i]); 
      } 


      } 
} 
1

あなたの問題は、この行です:

keywords.entrySet().iterator().next().getValue() 

は常にkeywordsのHashMapの同じエントリを返すために起こっています。以下のようなもので、あなたの新しいハッシュマップを構築してみてください。

for (int i = 1; i < 3; i++) { 
    tempHash.put(catnamecatkeys.get(i), keywords.get(i)); 
} 
0

あなたが持っているあなたの例によると:

hashmap1 = {1=e1, 2=e2} 
hashmap2 = {10=word1-word2-word3, 20=word4-word5-word6} 
the result: 
hashmap3 = {e1=word1-word2-word3, e2=word4-word5-word6} 

hashmap1とhashmap2の間に共通のキーがないので、キー "1"のhashmap1の値とキー "10"のhashmap2の値を関連付けることを試みています。 hashmap1からhashmap2へのエントリのマッピング方法に関する追加情報が保持されないかぎり、これを行う方法はありません。この追加情報は、繰り返し順序を挿入順序と同じにするマップが使用されている場合(LinkedHashMapなど)、マップへの挿入順序になります。

関連する問題