Q:

Search a HashMap in an ArrayList of HashMap

I have an ArrayList of HashMap. I want to search a HashMap  in it but unable to find a way to achieve this. Please suggest me how it can be done?
Thanks.

A:

Answer to your question the way i understood it!
for (HashMap<String, String> hashMap : yourArrayList)
    {
        // For each hashmap, iterate over it
        for (Map.Entry<String, String> entry  : hashMap.entrySet())
        {
           // Do something with your entrySet, for example get the key.
           String sListName = entry.getKey();
        }
    }

Your Hashmap might use other types, this one uses Strings.

A:

See if this helps:
@Test
public void searchMap() {
    List<Map<String, String>> listOfMaps = new ArrayList<Map<String,String>>();
    Map<String, String> map1 = new HashMap<String, String>();
    map1.put("key1", "value1");
    Map<String, String> map2 = new HashMap<String, String>();
    map1.put("key2", "value2");
    Map<String, String> map3 = new HashMap<String, String>();
    map1.put("key3", "value3");
    listOfMaps.add(map1);
    listOfMaps.add(map2);
    listOfMaps.add(map3);

    String keyToSearch = "key2";
    for (Map<String, String> map : listOfMaps) {
        for (String key : map.keySet()) {
            if (keyToSearch.equals(key)) {
                System.out.println("Found : " + key + " / value : " + map.get(key));
            }
        }
    }
}

Cheers!

