6

我有一个 HashMap 的 ArrayList。我想在其中搜索 HashMap 但无法找到实现此目的的方法。请建议我怎么做?谢谢。

4

6 回答 6

7

用我理解的方式回答你的问题!

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

您的 Hashmap 可能使用其他类型,这个使用字符串。

于 2012-07-10T20:20:41.220 回答
4

看看这是否有帮助:

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

干杯!

于 2012-07-10T20:18:00.350 回答
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
}

如果您希望获取对 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
}
于 2012-07-10T20:18:11.800 回答
1

试试下面的改进代码来搜索 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;

}
于 2012-12-24T08:51:31.200 回答
0

我用来在数组列表中搜索哈希图而不使用循环的有效方式。由于循环使执行时间更长

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");
}
于 2012-08-31T08:05:53.277 回答
0

如果您有这样的 ArrayList:ArrayList<HashMap<String, String>> 并且您想比较 HashMap 中的值之一,请尝试此代码。

我用它来比较我的警报通知的设置。

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
         } 
      }
   }
}
于 2013-06-20T10:26:23.943 回答