Remove all entries from HashMap where value is NOT what I'm looking for

只愿长相守 提交于 2020-04-11 11:49:47

问题


The title question summarizes it nicely, but assume I am coding in Java and have a HashMap that looks something like this (it has a lot more entries obviously):

Map<String, Integer> myMap = new HashMap<>();

myMap.put{"a", 1}
myMap.put{"b", 2}
myMap.put{"c", 2}
myMap.put{"d", 3}

Now I don't like entries with the value of 2, so I want to remove them all as effectively as possible, leaving me only with the entries that have value 1 or 3.

It should look as if my map was instead made like this:

Map<String, Integer> myMap = new HashMap<>();

myMap.put{"a", 1}
myMap.put{"d", 3}

As if my 2-valued entries were never there at all!

What are my options for doing this in an effective way?


回答1:


map.entrySet().removeIf(entry -> entry.getValue() == 2)



回答2:


Using helper map

If you want to do it as effectively as possible you can create an inverted map:

HashMap<Integer, List<String>> affectedKeysMap = new HashMap<>();

You'll need to track all updates to both maps manually to keep them in sync.

This will allow you to find and remove affected keys in an effective way:

int valueToRemove = 2;
for(String affectedKey : affectedKeysMap.get(2)) {
    map.remove(affectedKey);
}
affectedKeysMap.remove(valueToRemove);

Using single map

With a single HashMap all you can do is traverse the whole set of entries because HashMap doesn't support fast search by value.

This question already covers different approaches with a single map: Removing all items of a given value from a hashmap

Using something else

Another option is to look for a specialized collection class that supports bidirectional search and removal. Check out these discussions for possible leads:

  • Bidirectional multi-valued map in Java
  • Do we have a MultiBiMap ?



回答3:


Using guava you could do it like this:

Integer valueToDrop = Integer.valueOf(2);
Predicate<Integer> predicate = Predicates.not(Predicates.equalTo(valueToDrop));
Map<String, Integer> filteredMap = Maps.filterValues(myMap, predicate);


来源:https://stackoverflow.com/questions/44041137/remove-all-entries-from-hashmap-where-value-is-not-what-im-looking-for

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!