1

I Have a ArrayList as ArrayList<HashMap<String, String>> arrayList_LatLong = new ArrayList<HashMap<String, String>>(); in which I put HashMap into that ArrayList. The HashMap key-value pair suppose as follows

map.put(key1,value1);  
map.put(key2,value2);  
map.put(key3,value3);  
map.put(key4,value4);  
map.put(key5,value5);  

Now I want ArrayList Index according to Value1, that means I have Value1 and I want the respective ArrayList Index so that I can get Other Values also from the respective HashMap.

1
  • Explain with example.. Commented Sep 6, 2013 at 8:37

4 Answers 4

4

try this--->

public static int getIndexOFValue(String value, List<Map<String, String>> listMap) {

    int i = 0;
    for (Map<String, String> map : listMap) {
        if (map.containsValue(value)) {
            return i;
        } 
        i++;
    }
    return -1;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you just need to defined it this way :

ArrayList<HashMap<String, Integer>> arrayList_LatLong = new ArrayList<HashMap<String, Integer>>();

When you want to get the index in the list you just gotta do :

Integer newIndex = arrayList_LatLong.get(someIndex).get("aKey");
HashMap<String, Integer> indexes = arrayList_LatLong.get(newIndex);

Comments

0

You can do something like:

Here i am searching for the value "android". you can change it according to your code

String value = "android";

int getIndex(){
  int i = -1;
  for (Map<String, String> map : arrayList_LatLong) {
    for (String key : map.keySet()) {
      if (map.get(key).equals(value)) {

          i = arrayList_LatLong.indexOf(map);

      }
   } 
}

return i;
}

Comments

0

public static int getIndexOFkey(String key, ArrayList> listMap) {

    int i = 0;
    for (i=0; i<listMap.size(); i++)
    {
        if(listMap.get(i).get("id").equalsIgnoreCase(key))
        {
         return i;
        }
    }

    return -1;
}

1 Comment

How about indenting the first line and commenting a bit the code?

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.