2012-07-10 109 views
6

我有一個HashMap ArrayList。我想在其中搜索一個HashMap,但無法找到實現此目的的方法。請告訴我如何做到這一點? 謝謝。在HashMap的ArrayList中搜索HashMap

+0

遍歷ArrayList中的所有'HashMap'並檢查一個又一個的key? – 2012-07-10 19:59:53

+0

是否要在地圖列表中搜索單個地圖?你如何認識到你必須搜索的單一地圖? – eran 2012-07-10 20:00:14

+0

將其保存到數據庫並使用SQL進行搜索?用Lucene索引並搜索全文?轉化爲逆向索引的紅黑樹?根據你所說的,誰知道...... – 2012-07-10 20:09:34

回答

5

回答到你的問題是我理解它的方式!

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

您的散列圖可能使用其他類型,這個使用了字符串。

+0

迭代你在列表中間找到關鍵字的整個列表是沒有意義的。 – 2012-12-24 08:52:12

3

看看這有助於:

@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。由於循環使執行時間更長

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

請嘗試以下改進的代碼,用於在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; 

} 
+0

將密鑰作爲需要搜索的參數傳遞。 – 2012-12-24 08:52:50

0

如果你有像這樣的一個ArrayList:的ArrayList <的HashMap <字符串,字符串> > ,你要比較的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 
     } 
     } 
    } 
}