【问题标题】:Search a HashMap in an ArrayList of HashMap在 HashMap 的 ArrayList 中搜索 HashMap
【发布时间】:2012-07-10 19:56:35
【问题描述】:

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

【问题讨论】:

  • ArrayList 中遍历所有HashMaps 并依次检查密钥?
  • 您想在地图列表中搜索单个地图吗?您将如何识别您必须搜索的那张地图?
  • 保存到数据库并用SQL搜索?用 Lucene 索引它并搜索全文?转换成反向索引的红黑树?根据你说的,谁知道……
  • 实际上我有两个地图列表。我正在将地图从第一个列表移到第二个。我不想要第二个列表中的重复地图。这就是为什么我想在添加之前检查天气地图是否存在于列表中。
  • 我试过了。但它不起作用,因为只有当两个变量引用相同的对象时它才返回 true。

标签: java


【解决方案1】:

看看这是否有帮助:

@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】:

    按照我的理解回答你的问题!

    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 可能使用其他类型,这个使用字符串。

    【讨论】:

    • 遍历整个列表是没有意义的,你在列表中间找到了键。
    【解决方案3】:

    我用来在数组列表中搜索哈希图而不使用循环的有效方法。由于循环使执行时间更长

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

    【讨论】:

    • 这太荒谬了,如果我已经有了地图对象,为什么还要从列表中再次获取它?
    【解决方案4】:
    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
    }
    

    【讨论】:

      【解决方案5】:

      试试下面的改进代码来搜索 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;
      
      }
      

      【讨论】:

      • 将键作为需要搜索的参数传递。
      【解决方案6】:

      如果你有一个这样的 ArrayList:ArrayList> 并且您想比较 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
               } 
            }
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-14
        • 1970-01-01
        • 1970-01-01
        • 2011-10-06
        • 1970-01-01
        相关资源
        最近更新 更多