java.util.Set 中的重复元素 [英] Duplicate elements in java.util.Set

查看:17
本文介绍了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.

这篇关于java.util.Set 中的重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆