2016-10-09 9 views
1

値としてHashMapを含むHashMapがあります。値として考えられるHashMapにキーペア値を追加したいと思います。私はこのような何かを書いたHashMapのHashMapに要素を追加する

HashMap<String, HashMap<String, Integer>> record= new HashMap<>(); 
record.put("John",....)// I am not sure what to put here 

どのように行うことができますか?だから、

答えて

2
//get innerMap using key for record map 
innerMap = record.get("John"); 
if(innerMap == null){ // do not create new innerMap everyTime, only when it is null 
    innerMap = new HashMap<String, Integer>(); 
} 
innerMap.put("Key", 6); // put using key for the second/inner map 
record.put("John", innerMap) 
2

、その値は次のように格納することがあります。

HashMap<String,Integer> value = new HashMap<>(); 
value.put("Your string",56); 

次に、このようなあなたのレコードハッシュマップには、この値のハッシュマップを追加します。

record.put("John",value); 
+0

を取得する方法整数のための "あなたの整数" 文句を言わない仕事です。フォーマットを確認してください。 – Jonatan

+0

うん、確かに、私はそれが目的を説明するために大丈夫だと思う:P –

2
HashMap<String, HashMap<String, Integer>> record= new HashMap<>(); 
HashMap hm = new HashMap<>(); 
hm.put("string", 1); 
record.put("John", hm); 
1

まずあなたが持っていますHashMapのインスタンスを取得する

HashMap<String, Integer> map = new HashMap<>(); 
map.put("key", 1); 
あなたはこのように使用することができます

その後、

recore.put("John", map); 
1

-

HashMap<String, HashMap<String, Integer>> record= new HashMap<String, HashMap<String, Integer>>(); 

    HashMap<String, Integer> subRecord = new HashMap<String, Integer>(); 
    subRecord.put("Maths", 90); 
    subRecord.put("English", 85); 

    record.put("John",subRecord); 
0

あなたはこれを参照してください内部ハッシュマップから値を取得する方法についての情報が必要な場合、私は多くの答えを見てきました。

HashMap<String, HashMap<String, Integer>> record= new HashMap<>(); 
    Map<String, Integer> innerMap = new HashMap<String, Integer>();  
    innerMap.put("InnerKey1", 1); 
    innerMap.put("InnerKey2", 2); 

外側のHashMapに値を格納する

record.put("OuterKey", innerMap); 

これは、あなたが値

Map<String, Integer> map = record.get("OuterKey"); 
    Integer myValue1 = map.get("InnerKey1"); 
    Integer myValue2 = map.get("InnerKey2"); 
関連する問題