我想了解包含HashMap,并希望能得到一些帮助去除值(在这种情况下,地方)。我有3个包含HashMap(perName,perCategory,perPosition),其中2个具有地方作为值列表和所有有弦/位置作为键。因此,可能有不同的地方具有相同的名称或类别。我想从每个HashMap的值列表中删除某个地方,根据该地点是否被“标记”。如何从HashMap的在Java值的列表中删除一个值?
每增加地方被存储在所有包含HashMap和标记的地方被存储在一个单独的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();