java 在 HashMap 的 ArrayList 中搜索 HashMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11420920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:03:22  来源:igfitidea点击:

Search a HashMap in an ArrayList of HashMap

java

提问by Ashish Kumar

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.

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

回答by Lucas Arrefelt

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.

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

回答by Eduardo Andrade

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!

干杯!

回答by Salil Pandit

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 而不是对象的引用(无论出于何种原因),同样的过程适用,除了您只存储对映射的引用:

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
}

回答by Pattabi Raman

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

回答by Rais Alam

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

尝试使用以下改进的代码来搜索 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;

}

回答by DeeFour

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.

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

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