4

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?

ViRALiC
  • 1,419
  • 4
  • 18
  • 46

3 Answers3

14
map.entrySet().removeIf(entry -> entry.getValue() == 2)
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • This did it! I had to go with `map.entrySet().removeIf(e -> (int) ((Entry) e).getValue() == 2)` but the principle is the same. – ViRALiC May 18 '17 at 07:44
1

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:

default locale
  • 13,035
  • 13
  • 56
  • 62
  • 1
    Great answer, but sadly not the first one to it. This gave me plenty of reading though, so a juicy vote-up for you! – ViRALiC May 18 '17 at 07:44
0

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);