通过传递键列表 - Java API 方法或实用程序从 HashMap 中删除键?

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

我有一个 Map<String, String> issueMap 有 n 个值

和具有 m 个值的 An ArrayList<String>,使得 m 是 n

的子集

我想从 issueMap 中删除所有这些 m 键是否有直接 API 调用此

谢谢

您可以从 keySet 中删除密钥:

issueMap.keySet().removeAll(listOfKeysToRemove);

keySet returns Map 中包含的一个 Set 键,由 Map 支持。因此,对 Map 的更改会反映在 Set 中,反之亦然。

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: 此映射中包含的键的集合视图