【问题标题】:How to remove null { } map from ArrayList<HashMap<String, String>>?如何从 ArrayList<HashMap<String, String>> 中删除 null { } 映射?
【发布时间】:2013-05-11 01:02:13
【问题描述】:

我有一个ArrayList&lt;HashMap&lt;String, String&gt;&gt; placesListItems

当我从placesListItems 中删除地图时,空地图仍然存在。这样我的ListAdapter 包含空列表项。

for (HashMap<String, String> map : placesListItems) {
  for (Entry<String, String> entry : map.entrySet()) {
    for (int j = 0; j < duplicateList.size(); j++) {
      if (entry.getValue().equals(duplicateList.get(j))) {
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
          Entry<String, String> pairs = (Entry)iterator.next();
          System.out.println(pairs.getKey() + " = " + pairs.getValue());
          iterator.remove(); // avoids a ConcurrentModificationException
        }
      }
    }
  }
}     
ListAdapter adapter = new ItemAdapterHome(getApplicationContext, placesListItems);
lv.setAdapter(adapter); 

我该如何解决这个问题?

【问题讨论】:

  • 你能对你的缩进做点什么吗?
  • 您永远不会从 placesListItems 列表中删除任何内容。
  • 从地图中删除键值,而不是值。

标签: java iterator hashmap


【解决方案1】:

您需要做的是在列表中添加所有空地图并在最后将它们全部删除。

List<HashMap<String, String>> mapsToRemove= new ArrayList<HashMap<String, String>>();//list in which maps to be removed will be added
for (HashMap<String, String> map : placesListItems)
   {
    for (Entry<String, String> entry : map.entrySet())
     {
      for (int j = 0; j < duplicateList.size(); j++) 
          {
        if(entry.getValue().equals(duplicateList.get(j)))
         {
          Iterator iterator = map.entrySet().iterator();
          while (iterator.hasNext()) 
               {
              Entry<String, String> pairs = (Entry)iterator.next();
              System.out.println(pairs.getKey() + " = " + pairs.getValue());
              iterator.remove(); // avoids a ConcurrentModificationException                   }
               }
          }
      }
      if(map.isEmpty){//after all above processing, check if map is empty
         mapsToRemove.add(map);//add map to be removed
      }
 }  
placesListItems.removeAll(mapsToRemove);//remove all empty maps


ListAdapter adapter = new ItemAdapterHome(getApplicationContext, placesListItems);
lv.setAdapter(adapter); 

您可能需要根据您的要求稍微更改逻辑。

【讨论】:

    【解决方案2】:

    您的问题是您正在清空地图而不是从列表中删除它们。

    尝试以下方法:

    Iterator<Map<String, String>> iterator = placesListItems.iterator();
    while (iterator.hasNext()) {
        Map<String, String> map = iterator.next();
        for (String value : map.values()) {
            if (duplicateList.contains(value)) { // You can iterate over duplicateList, but List.contains() is a nice shorthand.
                iterator.remove(); // Removing the map from placesListItems
                break; // There is no point iterating over other values of map
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      相关资源
      最近更新 更多