Java 从哈希图中删除给定值的所有项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2594059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing all items of a given value from a hashmap
提问by Anthony Webb
So I have a java hashmap like below:
所以我有一个像下面这样的java hashmap:
hMap.put("1", "One");
hMap.put("2", "Two");
hMap.put("3", "Two");
I would like to remove ALL items where the value is "Two"
我想删除值为“两个”的所有项目
If I do something like:
如果我做这样的事情:
hmap.values().remove("Two");
Only the first one is deleted, I want to remove them all, how can this be done?
只删除了第一个,我想全部删除,怎么办?
采纳答案by Kevin Bourrillion
hmap.values().removeAll(Collections.singleton("Two"));
hmap.values().removeAll(Collections.singleton("Two"));
EDIT:the (significant) disadvantage with this concise approach is that you are basically forced to comment it, saying something like
编辑:这种简洁方法的(显着)缺点是你基本上被迫评论它,说类似的话
// remove("Two") would only remove the first one
// remove("Two") would only remove the first one
otherwise, some well-meaning engineer will try to simplify it for you someday and break it. This happens... sometimes the well-intentioned do-gooder is even Future You!
否则,某天某个善意的工程师会尝试为您简化并打破它。这种情况会发生……有时,善意的行善者甚至是未来的你!
回答by Ron
for (Iterator<Map.Entry<String,String>> it = hMap.entrySet().iterator(); it.hasNext();) {
Map.Entry<String,String> e = it.next();
if ("Two".equals(e.getValue())) {
it.remove();
}
}
回答by Marcus Leon
You have to iterate through the list, look at the value object, and conditionally do the remove. Note you'll get an exception if you try to remove an object while iterating over a HashMap
. Would have to make a copy of the map or use ConcurrentHashMap
.
您必须遍历列表,查看值对象,并有条件地进行删除。请注意,如果您尝试在迭代HashMap
. 将不得不制作地图的副本或使用ConcurrentHashMap
.
回答by sgtFloyd
You can use while( hmap.values().remove("Two") );
since the remove call returns true
if the collection was changed as a result of the call.
如果集合因while( hmap.values().remove("Two") );
调用true
而更改,则您可以使用因为 remove 调用返回。
回答by Jon Quarfoth
(Updated solution for logging of removed values)
(用于记录已删除值的更新解决方案)
This solution uses the google-collections library [LINK]
此解决方案使用 google-collections 库 [ LINK]
import static com.google.common.collect.Maps.filterValues;
import static com.google.common.base.Predicates.equalTo;
...
Map<String, String> removedValues = filterValues(hMap, equalTo("Two"));
System.out.println(removedValues); //Log Removed Values
removedValues.clear(); //Removes from original map, since this is a view.
Note - This solution takes advantage of the fact that the Map returned by the filterValues call is a view of the elements in the original HashMap. This allows us to examine them and log out the keys that were removed, and then remove them from the original map with a simple call to clear().
注 - 此解决方案利用了 filterValues 调用返回的 Map 是原始 HashMap 中元素的视图这一事实。这允许我们检查它们并注销被删除的键,然后通过简单地调用 clear() 从原始映射中删除它们。
You may have reasons for not wanting to use the google-collections library in your project, but if you don't, I'd suggest checking it out.
您可能有理由不想在您的项目中使用 google-collections 库,但如果您不这样做,我建议您查看一下。
回答by gpanders
In Java 8
在 Java 8 中
hmap.values().removeIf(val -> "Two".equals(val));