这是我的代码:
public static HashMap<String, ArrayList<String>> CountryMap = new HashMap<String, ArrayList<String>>();
ArrayList<String> stateInd = new ArrayList<String>();
ArrayList<String> stateUSA = new ArrayList<String>();
stateInd.add("GJ");
stateInd.add("MP");
stateUSA.add("NJ");
stateUSA.add("NY");
CountryMap.put("India",stateInd);
CountryMap.put("USA",stateUSA);
for(int i=0;i<CountryMap.size();i++){
for (Entry<String, ArrayList<String>> entry : CountryMap.entrySet()) {
if (entry.getKey().equals("India") {
ArrayList<String> result = new ArrayList<String>();
# how to add Value of CountryMap with "India" as a Key to result ArrayList??
}
}
}
我想将值添加到 HashMap 中的另一个以“印度”为键的 ArrayList 中?
我试过了..
result.add(CountryMap.get("India"));
但是没有成功。它添加了所有值。
最佳答案
解决方案1:
result.addAll(CountryMap.get("India"));
解决方案2:
result.add(CountryMap.get("India").toString());
请尝试查看所有函数和引发的异常。如果有效请回复
关于java - 如何使用 Hashmap<String, String> 中的键检索 Value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11243795/