java - 如何使用 Hashmap<String, String> 中的键检索 Value?

标签 java android hashmap

这是我的代码:

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/

相关文章:

android - War目录无法解析

hashmap - 不能将变量借用为可变的,因为在构建自引用 HashMap 时它也被借用为不可变的

java - 属性值的组合

java - EclipseLink JPA : remove and persist in the same transaction, 约束错误

java - Vaadin JPAContainer 和树拖放

Java/Android 自动解析泛型类型。寻求逐步指导以了解

android - 从调用返回时 ViewRootImpl 中的 NullPointerException(仅适用于 Samsung Touch)

java - 我如何通过 for each 循环放置 HashMap (这将允许放置多个 HashMap )?

java - 使用 JUnit 的内部类中的测试用例

java - 包含来自外部数据库的数据的 ListView