本文介绍了从哈希映射中删除所有给定值的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 所以我有一个如下所示的java hashmap: hMap.put(1,One); hMap.put(2,Two); hMap.put(3,Two); 我想删除其中值为Two的所有项目 如果我这样做: hmap.values()。remove(Two) ; 只删除第一个,我想将它们全部删除,这怎么办? / p> 解决方案 hmap.values()。removeAll(Collections.singleton(Two)); $ b 编辑:这个简洁方法的(显着)缺点是你基本上被迫评论它,像 // remove(Two)只会删除第一个 $ b $否则,一些善意的工程师会在某天为你简化它并打破它。发生这种情况......有时候,善意的做人甚至是未来你! So I have a java hashmap like below: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? 解决方案 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 oneotherwise, 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! 这篇关于从哈希映射中删除所有给定值的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-01 06:59