Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove keys from HashMap by passing a List of keys - Java API method or Utility for the same?

I have a Map<String, String> issueMap with n values

and An ArrayList<String> with m values, such that m is subset of n

I want to remove all these m keys from issueMap is there a direct API call for this

Thanks

like image 678
SHinny Avatar asked Dec 01 '22 00:12

SHinny


1 Answers

You can remove the keys from the keySet :

issueMap.keySet().removeAll(listOfKeysToRemove);

keySet returns a Set of the keys contained in the Map, which is backed by the Map. Therefore, changes to the Map are reflected in the Set and vice-versa.

Javadoc:

Set keySet()

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Returns: a set view of the keys contained in this map

like image 146
Eran Avatar answered Dec 04 '22 11:12

Eran