【问题标题】:How to iterate through LinkedHashMap with lists as values如何使用列表作为值遍历 LinkedHashMap
【发布时间】:2012-08-31 21:54:29
【问题描述】:

我有以下 LinkedHashMap 声明。

LinkedHashMap<String, ArrayList<String>> test1

我的意思是如何遍历这个哈希映射。 我想在下面执行此操作,为每个键获取相应的数组列表并针对该键一一打印数组列表的值。

我试过了,但 get 只返回字符串,

String key = iterator.next().toString();  
ArrayList<String> value = (ArrayList<String> )test1.get(key)

【问题讨论】:

  • LinkedHashMap 用于保存排序。问题和回答都没有提到排序。请将 LinkedHashMap 替换为 HashMap。这很令人困惑

标签: java collections linkedhashmap


【解决方案1】:
for (Map.Entry<String, ArrayList<String>> entry : test1.entrySet()) {
    String key = entry.getKey();
    ArrayList<String> value = entry.getValue();
    // now work with key and value...
}

顺便说一句,你真的应该把你的变量声明为接口类型,比如Map&lt;String, List&lt;String&gt;&gt;

【讨论】:

  • 顺便说一句,我想要有插入顺序的列表,我之前使用过 hashmap,但它弄乱了顺序。
  • 我并不是说不要使用 LinkedHashMap - 但通常最好的做法是声明 Map&lt;String, List&lt;String&gt;&gt; map = new LinkedHashMap&lt;String, List&lt;String&gt;&gt; 之类的东西。
  • 为什么entrySet返回集合时保证顺序?我知道在 LinkedHashMap 中保证了顺序,但是文档说集合的迭代器不能保证顺序
  • 那你为什么要在声明中保留 ArrayList 呢?
  • @Jonathan.,好问题。迟到总比不到好:在stackoverflow.com/a/2924143/423105查看答案
【解决方案2】:

我假设您的 get 语句中有错字,应该是 test1.get(key)。如果是这样,我不确定它为什么不返回 ArrayList,除非您首先没有在地图中输入正确的类型。

这应该可行:

// populate the map
Map<String, List<String>> test1 = new LinkedHashMap<String, List<String>>();
test1.put("key1", new ArrayList<String>());
test1.put("key2", new ArrayList<String>());

// loop over the set using an entry set
for( Map.Entry<String,List<String>> entry : test1.entrySet()){
  String key = entry.getKey();
  List<String>value = entry.getValue();
  // ...
}

或者你可以使用

// second alternative - loop over the keys and get the value per key
for( String key : test1.keySet() ){
  List<String>value = test1.get(key);
  // ...
}

在声明变量时(以及在泛型参数中),您应该使用接口名称,除非您有非常具体的原因来定义使用实现。

【讨论】:

  • 您好,我使用 linkhashmap 将元素保持在插入顺序中。
  • 当然 - 但您可以在创建实例时指定 LinkedHashMap。但是,使用变量的接口名称可以使代码实现独立。即:您可以在以后轻松地用其他东西替换实现,而无需重新编码所有内容。使用接口作为 var 的声明和实现的 LinkedHashMap 参见我上面的示例。
【解决方案3】:

在 Java 8 中:

Map<String, List<String>> test1 = new LinkedHashMap<String, List<String>>();
test1.forEach((key,value) -> {
    System.out.println(key + " -> " + value);
});

【讨论】:

    【解决方案4】:

    您可以使用条目集并遍历条目,这样您就可以直接访问键和值。

    for (Entry<String, ArrayList<String>> entry : test1.entrySet()) {
         System.out.println(entry.getKey() + "/" + entry.getValue());
    }
    

    我试过了,但得到的只是返回字符串

    你为什么这么认为?方法get 返回选择泛型类型参数的类型E,在您的情况下为ArrayList&lt;String&gt;

    【讨论】:

    • 嘿,test1.entrySet() 后面少了一个括号,但由于编辑少于 6 个字符,我无法完成,只是想为以后的用户指出这一点
    • 我更新了,谢谢。
    【解决方案5】:
    // iterate over the map
    for(Entry<String, ArrayList<String>> entry : test1.entrySet()){
        // iterate over each entry
        for(String item : entry.getValue()){
            // print the map's key with each value in the ArrayList
            System.out.println(entry.getKey() + ": " + item);
        }
    }
    

    【讨论】:

    • 没想到这个,我在C#里用过foreach,和那个差不多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 2019-02-14
    相关资源
    最近更新 更多