如何在Java中从HashMap中删除带有值列表的值?

4
我正在学习HashMap,并希望得到一些有关删除值(在本例中为地点)的帮助。我有3个HashMaps(perName,perCategory,perPosition),其中2个具有地点列表作为值,所有键都是字符串/位置。因此,可能会有相同名称或类别的不同地点。我想根据该地点是否“标记”来从每个HashMap的值列表中删除特定地点。
每个添加的地点都存储在所有HashMaps中,标记的地点存储在单独的ArrayList中。我试图遍历ArrayList并从每个HashMap中删除其每个地点/值。我不想删除键,因为可能有其他未标记的地点具有相同的键。这可能吗?目前当我尝试删除它们时,这些地点似乎不会消失。
我尝试了不同的方法,迭代器的方法(可能不必要)似乎会删除整个键,而使用for循环的方法则根本不会删除任何内容。如果我的信息不足以解决这个问题,请告诉我。谢谢。
    private Map<Position, Place> perPosition = new HashMap<>();
    private Map<String, List<Place>> perCategory = new HashMap<>();
    private Map<String, List<Place>> perName = new HashMap<>()

     //method 1
    Iterator<Place> it = markedPlace.iterator();
                 while (it.hasNext()) {

                 Place p = it.next();
                 perPosition.remove(p.getPosition());
                 perName.remove(p.getName());
                 perCategory.remove(p.getCategory());
                 p.setMarked(false);
                 p.setVisible(false);
                 it.remove();
            }

  //method 2
                for (Place p : markedPlace) {
                    for (Map.Entry<String, List<Place>> entry : perName.entrySet()) {
                        if (entry.getValue().equals(p)) {
                            perName.remove(entry.getValue());
                        }
                    }

                    for (Map.Entry<String, List<Place>> entry : perCategory.entrySet()) {
                        if (entry.getValue().equals(p)) {
                            perCategory.remove(entry.getValue());
                        }
                    }

                    for (Map.Entry<Position, Place> entry : perPosition.entrySet()) {
                        if (entry.getValue().equals(p)) {
                            perName.remove(entry.getKey());
                        }

                        }
                   markedPlace.clear();
3个回答

4

entry.getValue()的类型是List<Place>,而p的类型是Place。因此,在循环perNameperCategory时,等式永远不会成立。你需要使用.contains(foo)来检查对象是否在列表中。

然后,你试图清除你想要保留的列表。你需要从列表中删除条目,而不是从映射中删除。

if (entry.getValue().contains(p)) {
  entry.getValue().remove(p);
}

然后,列表支持删除集合,因此您可以简单执行以下操作:

for (Map.Entry<String, List<Place>> entry : perName.entrySet()) {
    entry.getValue().remove(markedPlace);
}

此外,在Place类中,确保覆盖equals和hashcode方法,否则它只会匹配两个Place对象是完全相同的实例化对象。

0

0
Two HashMap given below-

hmap2 - >{Competitor=[aaa, bbb, 000, 111], Contractor=[ccc, ddd, 222, 333]}
hmap 1 ->{Competitor=[aaa, bbb], Contractor=[ccc, ddd]}

Below is the code to remove hmap1 from hmap2 - > 

for (Map.Entry<String, List<String>> entry : hmap2.entrySet()) {
            for (Map.Entry<String, List<String>> tobeDeleted : hmap1.entrySet()) {
                List<String> li = tobeDeleted.getValue();
                for (String li1 : li) {
                    entry.getValue().remove(li1);
                }
            }

        }
        System.out.println(hmap2);




Output - >{Competitor=[000, 111], Contractor=[222, 333]}

Happy Learning :)

你的回答目前写得不清楚。请[编辑]以添加更多细节,帮助其他人理解这个回答如何解决所提出的问题。你可以在帮助中心找到关于如何撰写良好回答的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接