我正在尝试对 map 应用过滤器。目的是仅保留属于一组的那些键。以下实现确实提供了所需的结果,但我想知道这是否是正确的方法?
private void filterProperties(Map<String, Serializable> properties, Set<String> filterSet) {
Set<String> keys = properties.keySet();
keys.retainAll(filterSet);
}
最佳答案
是的!
The set is backed by the map, so changes to the map are reflected in the set, and vice-versa
(参见:https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/HashMap.html#keySet())
关于java - 我可以通过Keyset修改 map 吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5781083/