2016-02-04 61 views
1

我想添加一个Key(String),Value(HashMap)到另一个HashMap。我不知何故在这里保持语法和逻辑的混乱。我怎么做?我这里有KMAP初始化,然后我想补充的一个关键这是一个字符串,和值是另一个HashMap<String, List<Integer>>添加一个HashMap作为一个值的HashMap

这些可以在下面的参数可以看出:

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

public synchronized static void AddMapToList_ofMAP_(HashMap<String, List<Integer>> value, String key) { 

    if (!kmap.containsKey(key)) { 
     kmap.put(key, new HashMap<String, List<Integer>>()); 
    } 
    HashMap<String, List<Integer>> q = kmap.get(key); 

    q.put(key, value); 
} 
+0

是什么问题? – Vaseph

+1

您遇到的问题是什么?读取语法似乎是正确的 – yugo

+0

因为'q'是'Map >',所以'q.put(key,value)'要添加的'value'应该是'List '。 – Kenney

回答

1

这是WAY简单的比你使它看起来。您不需要单独的方法,只需在您的kmap上调用put(key, value)方法即可。假设你已经在一个名为value变量Map<String, List<Integer>>,它只是:

kmap.put(key, value); 

这就是全部。只有一条线。

+1

你是对的,我让它更复杂,然后它需要。一条投入线就是做我所需要的东西,谢谢你 – dedpo

1

在您的参数你有一个名为value的HashMap。然后你试图把它添加到HashMap中的HashMap中,但是它的值需要是一个整数列表。

修复:

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

public synchronized static void AddMapToList_ofMAP_(
     List<Integer> value, String key) { 

    if (!kmap.containsKey(key)) { 
     kmap.put(key, new HashMap<String, List<Integer>>()); 
    } 
     HashMap<String, List<Integer>> q = kmap.get(key); 

     q.put(key, value); 
    } 

而且,一个可行的办法,使这更好​​的使用对象。我不确定代码和你的投入,但是一个对象可以工作。

我也看到你通过键获得HashMap,但你也把这个键放在HashMap(里面的那个)中,当然你可能只有1个HashMap。

-1

的逻辑并不清楚,但也许你想这个

q.put(key, value.get(key)); 

,而不是这样的:

q.put(key, value); 
1

我不确定你到底想要达到什么目的。以下是您可能想要做的事情。随意编写更多测试用例并优化代码。但是,这会给你一个基础结构来处理。

公共类{#1

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

public synchronized static void addIntegerToKmap(String kmapKey, String intMapKey, Integer value) { 
    if (!kmap.containsKey(kmapKey)) { 
     Map<String, List<Integer>> intMap = new HashMap<String, List<Integer>>(); 

     HashMap<String, List<Integer>> stringListHashMap = new HashMap<String, List<Integer>>(); 
     List<Integer> integerList = new ArrayList<Integer>(); 

     integerList.add(value); 
     stringListHashMap.put(intMapKey, integerList); 
     kmap.put(kmapKey, stringListHashMap); 
    } 
    else { 
     HashMap<String, List<Integer>> stringListHashMap = kmap.get(kmapKey); 
     List<Integer> integerList = stringListHashMap.get(intMapKey); 
     if (integerList != null && !integerList.isEmpty()) { 
      integerList.add(value); 
     } 
     else { 
      integerList = new ArrayList<Integer>(); 
      integerList.add(value); 
      stringListHashMap.put(intMapKey, integerList); 
     } 
    } 
} 

public static void main(String args[]) { 
    addIntegerToKmap("A", "A1", 1); 
    addIntegerToKmap("A", "A1", 2); 

    addIntegerToKmap("A", "A2", 12); 
    addIntegerToKmap("A", "A2", 22); 

    addIntegerToKmap("B", "B1", 1); 

    addIntegerToKmap("A", "A1", 3); 
} 

}