0

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
2
  • Do you want to compare arraylist values with hashmap? Commented Jun 27, 2014 at 6:31
  • no I need to get value when I compare its key with tid... Commented Jun 27, 2014 at 6:37

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hi.. thank you.. this is working and showing all values but I need to get only that value whose key is being matched with tid... can you please help..
If the code is working then please up-vote the answer :P i will post another code in just a min
1

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

12 Comments

thank you... this is working and I am getting all values but I need to get only that value whose key is being matched with tid.. can you please help...
what is the value of tid?
it is dynamic.. it is either 27 or 23 or 22 or any other number which user sends. tid have the values which is also in entry.getKey()
@user3705697 but at a time tid value will be one from them,and it will match with key only match.then it will print all the values only once.So my answer will work.
yes only one value will be sent to this method and will be checked and value be will be returned to the other method
|
0

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()) {

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.