【发布时间】:2021-01-28 04:00:00
【问题描述】:
我有一个哈希图,它是 Map<String, Set<String>> tasks 。在任务中,我有一些键,键有一些值,而不仅仅是一个值。
我想用给定的键从 hashmap 中删除一个值,但我也想在那里保留其他值。
我的意思是像这样 release = ["fix bug 1", "fix bugs 2" , "implement feature X"] 。现在release 是键(在字符串类型中)并且具有值(在集合类型中)"fix bug 1", "fix bugs 2" , "implement feature X" .. 我想像这样修改 release:
release = ["implement feature X"]
如何删除 2 个值???谢谢
【问题讨论】:
-
您需要获取
Set<String>,然后像任何其他集合一样从中删除。Map对你正在做的事情并不重要。 -
但是当我从
Set<String>中删除值时,我必须更新hashMap,对吗? @LouisWasserman -
我收到了
ConcurrentModificationException异常 -
没有。您不应该更新地图。如果你得到
ConcurrentModificationException,那么你从集合中删除值的代码是错误的,可能是因为你在循环中调用set.remove(value)而不是使用Iterator.remove(),或者使用removeIf或removeAll不在循环中。