user3705697
user3705697

Reputation: 82

How to get values from arraylist Hashmap

How to get Value according to comparison. I am new in android and comparison is working fine but m not able to get value as it is displaying in arraylist. Below is my code and log-

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()));
        }
    }

The log -

   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

Upvotes: 0

Views: 165

Answers (3)

Giru Bhai
Giru Bhai

Reputation: 14398

To get values from Hashmap values(list of String),simply loop it and get all the values,So use your code as

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()));
        }
    }

Upvotes: 1

Kaustubh
Kaustubh

Reputation: 653

Here you are using nested maps. so just entry.getValue() will give you list. as you are iterating over Entry<Integer, List<String>>

Hence to get contents of List<String> which is your value for Integer you will have to do this

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
    }
}

Hope this works

Upvotes: 2

Rajen Raiyarela
Rajen Raiyarela

Reputation: 5636

You can iterate through the list you get in entry.getvalue() and get the values as below.

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

}

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

}

Upvotes: 0

Related Questions