6

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.

5
  • Iterate over all HashMaps in an ArrayList and check one after another for key? Commented Jul 10, 2012 at 19:59
  • Do you want to search a single map in a list of maps? How would you recognize that single map you have to search? Commented Jul 10, 2012 at 20:00
  • Save it to a database and search it with SQL? Index it with Lucene and search for full text? Transform into a reverse-indexed red-black tree? Based on what you said, who knows... Commented Jul 10, 2012 at 20:09
  • Actually i have Two lists of map. i am moving map from first list to second. I don't want the duplicate map in second List. that's why i want to check weather the map exist in the list before adding. Commented Jul 10, 2012 at 20:13
  • i tried. but it didn't work as it returns true only if the two variables are referencing same objects. Commented Jul 10, 2012 at 20:16

6 Answers 6

7

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.

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

1 Comment

It does not make sense to iterate entire list of you find the key in middle of list.
5

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!

Comments

2
Object myObj;
Object myKey;
//Traverse the list
for(HashMap curMap : listOfMaps){
    //If this map has the object, that is the key doesn't return a null object
    if( (myObj = curMap.get(myKey)) != null) {
         //Stop traversing because we are done
         break;
    }
}
//Act on the object
if(myObj != null) {
  //TODO: Do your logic here
}

If you are looking to get the reference to the Map instead of the object (for whatever reason) same process applies, except you just store the reference to the map:

Map myMap;
Object myKey;
//Traverse the list
for(HashMap curMap : listOfMaps){
    //If this map has the object, that is the key doesn't return a null object
    if(curMap.get(myKey) != null) {
         //Store instance to the map
         myMap = curMap;
         //Stop traversing because we are done
         break;
    }
}
//Act on the map
if(myMap != null) {
  //TODO: Do your logic here
}

Comments

1

Try below improved code for searching the key in a list of HashMap.

public static boolean searchInMap(String keyToSearch)
{
    boolean returnVal = false;
    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);

    for (Map<String, String> map : listOfMaps)
    {
        if(map.containsKey(keyToSearch))
        {
            returnVal =true;
                break;
        }

    }
    return returnVal;

}

1 Comment

Pass the keys as an argument which need to be searched.
0

The Efficient way i've used to search a hashmap in an arraylist without using loops. Since loop makes execution time longer

try{
int index = list.indexOf(map); // map is your map to find in ArrayList
if(index>=0){
HashMap<String, String> map = array_list.get(index);
// Here you can get your values
}
}
catch(Exception e){
e.printStackTrace();
Log.i("HashMap","Not Found");
}

1 Comment

This is ridiculous, if I already have the map object, why do I need to get it from the list again?
0

if you have an ArrayList like this one: ArrayList<HashMap<String, String>> and you want to compare one of the values inside the HashMap try this code.

I use it to compare settings of my alarm notifications.

for (HashMap<String, String> map : AlarmList) {
  for (String key : map.keySet()) 
  {
      if (key.equals("SendungsID")) 
      {
         if(map.get(key).equals(alarmMap.get("AlarmID")))
         {
               //found this value in ArrayList
         } 
      }
   }
}

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.