2017-08-14 7 views
-1

にハッシュマップを追加します。は、私は、静的なHashMapを持っている別の

protected void onResult(String srv, HashMap<String, byte[]> drawables) { 
     super.onResult(srv, drawables); 
     mDrawables.putAll(drawables); 
} 

しかしのputAllを呼び出したびに、mDrawables上のすべての情報がクリアされます。 どうすれば新しいマップキー、値を静的に追加できますか? Javadocをaccordint

+0

重複するキーがありますか? – Xvolks

+0

HashMapはスレッドセーフではありません。タイミングの問題から保護する必要があります。 – Xvolks

+0

@Xvolks、各キーは一意IDではありません –

答えて

1

まあ、:

/** 
* Copies all of the mappings from the specified map to this map. 
* These mappings will replace any mappings that this map had for 
* any of the keys currently in the specified map. 
* 
* @param m mappings to be stored in this map 
* @throws NullPointerException if the specified map is null 
*/ 

ので、同じキーが置き換えられます。 Map#put()を1サイクルで使用して、次のように自分で確認できます。

for (Map.Entry<String, byte[]> entry : drawables.entrySet()) { 
    if (mDrawables.containsKey(entry.getKey())) { 
     // duplicate key is found 
    } else { 
     mDrawables.put(entry.getKey(), entry.getValue()); 
    } 
} 
+0

。 'この行の中で(Map.Entry entry = drawables.entrySet()){' –

+0

申し訳ありません –

関連する問題