如何从arraylist Hashmap获取值

时间:2014-06-27 06:21:50

标签: arraylist hashmap

如何根据比较获得价值。我是android的新手,比较工作正常但是因为它在arraylist中显示而无法获得价值。下面是我的代码和日志 -

for (Entry<Integer, List<String>> entry : abbre.entrySet())
    {
        Log.d("Abbrevations values- ", entry.getKey() + " " + entry.getValue());
        if(tid.equals(String.valueOf(entry.getKey()))){
        //String aValue = String.valueOf(entry.getValue());
            Log.d("abbrKey Value: ", String.valueOf(entry.getValue()));
        }
        else{
            Log.d("abbrKey Key: ", String.valueOf(entry.getKey()));
        }
    }

日志 -

   06-27 11:27:39.375: D/abbrKey tid:(13602): 27
   06-27 11:27:39.375: D/Abbrevations values-(13602): 23 [8+, 4+, 2-]
   06-27 11:27:39.375: D/abbrKey Key:(13602): 23
   06-27 11:27:39.375: D/Abbrevations values-(13602): 27 [8+, 4+, 2-]
   06-27 11:27:39.375: D/abbrKey Value:(13602): [8+, 4+, 2-]
   06-27 11:27:39.375: D/Abbrevations values-(13602): 22 [8+, 4+, 2-]
   06-27 11:27:39.375: D/abbrKey Key:(13602): 22

3 个答案:

答案 0 :(得分:2)

您在这里使用嵌套地图。所以只有entry.getValue()会给你列表。在迭代Entry<Integer, List<String>>

因此,要获得List<String>的内容Integer,您必须这样做

if(tid.equals(String.valueOf(entry.getKey()))){
    for (String str : entry.getValue()) {
     Log.i("abbrKey Value:", str);  
         // iterating through values which is list in this case
    }
}

希望这有效

答案 1 :(得分:1)

要从Hashmap值(String列表)中获取值,只需将其循环并获取所有值,因此请使用您的代码

for (Entry<Integer, List<String>> entry : abbre.entrySet())
    {
        Log.d("Abbrevations values- ", entry.getKey() + " " + entry.getValue());
        if(tid.equals(String.valueOf(entry.getKey()))){
        //String aValue = String.valueOf(entry.getValue());
            Log.d("abbrKey Value: ", String.valueOf(entry.getValue()));

         List<String> listValues = entry.getValue();
          for (int i = 0; i < listValues.size(); i++){
                String firstValue = listValues.get(i); 
               // similar get all values 
             }
        }
        else{
            Log.d("abbrKey Key: ", String.valueOf(entry.getKey()));
        }
    }

答案 2 :(得分:0)

您可以遍历entry.getvalue()中获得的列表,并获取以下值。

for (int i = 0; i < entry.getValue().size(); i++){

}

for (String s: entry.getValue()) {

}