java.util.Set 中的重复元素
java.util.Set
实现移除重复元素.
如何在 java.util.Set
内部删除重复元素?
How are duplicates elements deleted internally in a java.util.Set
?
推荐答案
实际上,来自 java 中的大多数 Set
实现的 AFAIK 甚至都不检查元素是否已包含.
Actually AFAIK from the sources most Set
implementations in java don't even check if the element is already contained.
他们只是总是在其内部结构上执行 add()
来保存集合元素并让该对象处理重复情况.
They just always execute the add()
on their internal structure which holds the set elements and let that object handle the duplication case.
例如HashSet
在内部 HashMap
上调用 put(K,V)
,如果重复,则插入新对象覆盖旧条目.
e.g. HashSet
calls put(K,V)
on the internal HashMap
which just inserts the new object overwriting the old entry if duplicate.
相关文章