2012-07-10 21 views
6

私はHashMapのArrayListを持っています。私はその中のHashMapを検索したいが、これを達成する方法を見つけることができない。どのようにそれを行うことができますか教えてください?おかげさまで HashMapのArrayListでHashMapを検索する

+0

'' ArrayList'の 'HashMap'sをすべて繰り返し、キーを次々にチェックしますか? –

+0

地図のリストで1つの地図を検索しますか?検索しなければならない単一の地図をどのように認識しますか? – eran

+0

データベースに保存してSQLで検索しますか? Luceneで索引付けし、フルテキストを検索しますか?逆インデックスの赤黒の木に変換しますか?あなたが言ったことに基づいて、誰が知っている... –

答えて

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

リストの途中でキーを見つけたリスト全体を反復するのは意味がありません。 –

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

ループを使用せずにarraylistでハッシュマップを検索するのに便利な方法です。ループが実行時間を長くするので、

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

キーを引数として渡す必要があります。 –

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 
     } 
     } 
    } 
} 
関連する問題