簡體   English   中英

在HashMap的ArrayList中搜索HashMap

[英]Search a HashMap in an ArrayList of HashMap

我有一個HashMap的ArrayList。 我想在其中搜索HashMap但無法找到實現此目的的方法。 請建議我怎么做? 謝謝。

以我理解的方式回答您的問題!

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可能使用其他類型,這個使用字符串。

看看這是否有幫助:

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

干杯!

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
}

我用來在不使用循環的情況下搜索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");
}

嘗試使用以下改進的代碼來搜索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;

}

如果您有一個像這樣的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
         } 
      }
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM