Hashmap的getValue返回Object

时间:2015-07-23 14:56:09

标签: java hashmap

我有以下数据结构:

CFU66=[{Bild1=CFU6606}, {Bild2=CFU6603}, {Bild3=CFU6605}, {Bild4=CFU6601}, {Bild5=CFU6602}]

结构:Hashmap_1(String Key, List(Hashmap_2(String Key, String Value)))

我正在尝试访问Hashmap_2

中的值
// for each Hashmap_1 entry
for (Map.Entry<String, List> csvDictEntry : csvDict.entrySet()) {
   // for each List in entry.getValue
   for (List<HashMap> hashList : csvDictEntry.getValue()) {
      // for each Hashmap_2 in List
      for (HashMap<String, String> hashListDict : hashList) {
         // for each entry in Hashmap_2 print Value
         for (Map.Entry<String, String> entry :hashListDict.entrySet()){
            System.out.println(entry.getValue());
         }
      }
   }
}

编译器给出消息,第二个for循环中的csvDictEntry.getValue()返回Object而不是Hashmap。为什么呢?

但是,我对Java很陌生,我确信有一种更方便的方法。

5 个答案:

答案 0 :(得分:4)

for (Map.Entry<String, List> csvDictEntry : csvDict.entrySet()) {

应该是

for (Map.Entry<String, List<Map<String, String>>> csvDictEntry : csvDict.entrySet()) {

答案 1 :(得分:2)

只需写下所有类型

    Map<String, List<HashMap<String, String>>> csvDict = null;

    for (Map.Entry<String, List<HashMap<String, String>>> csvDictEntry : csvDict.entrySet()) {
        // for each List in entry.getValue
        for (HashMap<String, String> hashList : csvDictEntry.getValue()) {
            // for each Hashmap_2 in List
            for (Map.Entry<String, String> entry : hashList.entrySet()) {
                System.out.println(entry.getValue());
            }
        }
    }

而且你还有额外的for循环。

答案 2 :(得分:1)

如其他人所示,您忘记使用&lt;&gt;在&#39; List&#39;之后在你的第一行代码上。因此,编译器不知道这些列表中的元素类型。 当你只对价值感兴趣时,你也可以通过迭代entrySet来使它变得不必要地复杂。

Map有3个迭代函数:

  1. keySet() - 如果您只对密钥感兴趣
  2. values() - 如果您只对价值感兴趣
  3. entrySet() - 如果您对两者都感兴趣
  4. 所以在你的情况下...

    for (Map<String,String> map : csvDict.values()) {
        for (String value : map.values()) {
            System.out.println(value);
        }
    }
    

答案 3 :(得分:1)

基于

for (Map.Entry<String, List> csvDictEntry : csvDict.entrySet()) {

我认为csvDict的类型是

Map<String, List> csvDict = ...;

根据您的数据示例CFU66=[{Bild1=CFU6606}, {Bild2=CFU6603}, {Bild3=CFU6605}, {Bild4=CFU6601}, {Bild5=CFU6602}]

应该是

Map<String, List<Map<String,String>>> csvDict = ...;

您的引用类型的问题是List是原始类型,这意味着它的实际类型是未知的(它可以存储任何类型的对象),因此Object是编译器在您使用时所采用的类型正试图迭代这样的列表,所以通常我们会期望

for (Type t : List<Type>)

我们正在获取原始类型

for (Object o : rawList)

其他问题是您正在迭代的方式,因为即使我们将您的引用更改为正确的类型

for (List<HashMap> hashList : csvDictEntry.getValue())

将无法编译,因为getValue()会返回List<HashMap>,因此您的循环会迭代HashMap,而不是List HashMap,因此它应该

for (HashMap hashList : csvDictEntry.getValue())

提示:尽量避免泛型中的具体类型,如果可能则使用父类型,如接口或抽象类型。这样您以后就可以轻松更改实际类型,例如从HashMapLinkedHashMap

所以你的迭代看起来应该是

for (Map.Entry<String, List<Map<String, String>>> csvDictEntry : csvDict.entrySet()) {
    // for each Map in List stored as value
    for (Map<String, String> hashListDict : csvDictEntry.getValue()) {
        // for each entry in hmap_2 print Value
        for (Map.Entry<String, String> entry : hashListDict.entrySet()) {
            System.out.println(entry.getValue());
        }
    }
}

DEMO

在Java 8中使用BTW,您的代码可以简化为

    csvDict.entrySet().stream()
    .flatMap(m -> m.getValue().stream())
    .flatMap(m -> m.values().stream())
    .forEach(System.out::println);

答案 4 :(得分:0)

您的问题是第一个问题,您目前正在做的是检索通用列表,而不是特定列表。

条目集的声明是:

 Set<Map.Entry<K,V>>   <-------     entrySet()
 Returns a Set view of the mappings contained in this map.

所以你应该重新考虑一下你的第一个:

 for (Map.Entry<String, List<HashMap>> csvDictEntry : csvDict.entrySet()) {