在一个HashMap的ArrayList中搜索一个HashMap

6

我有一个HashMap的ArrayList。我想在其中搜索一个HashMap,但无法找到实现这一目标的方法。请建议我如何做到这一点? 谢谢。


遍历ArrayList中的所有HashMap,逐个检查是否存在键。 - Tomasz Nurkiewicz
你想在地图列表中搜索单个地图吗?你如何识别需要搜索的那张地图? - Eran
将其保存到数据库并使用SQL进行搜索?使用Lucene进行索引并进行全文搜索?转换为反向索引的红黑树?基于你所说的,谁知道呢... - Marko Topolnik
实际上,我有两个映射列表。我正在将第一个列表中的映射移动到第二个列表中。我不希望第二个列表中有重复的映射。因此,在添加之前,我想检查映射是否存在于列表中。 - Ashish Kumar
我尝试过,但它并没有起作用,因为它仅在两个变量引用相同对象时返回true。 - Ashish Kumar
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();
        }
    }

你的哈希映射可能使用其他类型,而这个示例使用字符串。


如果在列表的中间找到了关键字,那么迭代整个列表就没有意义了。 - Rais Alam

5

看看这个是否有帮助:

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

干杯!


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 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
}

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;

}

将需要搜索的关键字作为参数传递。 - Rais Alam

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
         } 
      }
   }
}

0

我使用的在不使用循环的情况下搜索ArrayList中HashMap的高效方法。因为循环会使执行时间变长。

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

这太荒谬了,如果我已经有了地图对象,为什么还需要再从列表中获取它呢? - noname

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接