假设我有一个名为 myMap
的 HashMap,然后我从中获取一个 keySet
对象并修改 keySet
Set<Integer> keys = myMap.keySet();
keys.remove(1);
keys.remove(2);
如果 myMap 在此代码段之前包含 1
和 2
作为键,则执行此代码段后 myMap
会发生更改吗?
最佳答案
是的。从键集中删除元素会从映射中删除相应的条目。引用 Javadoc :
The set supports element removal, which removes the corresponding mapping from the map, via the
Iterator.remove
,Set.remove
,removeAll
,retainAll
, andclear
operations.
关于java - Java的HashMap中,从keySet中删除一个元素会影响hashmap对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43555886/