集合,使用什么方法来授权添加元素?

发布于 2024-09-02 11:33:04 字数 268 浏览 1 评论 0原文

我们在Collection下发现了很多具体的子类。

当尝试在具体集合中添加元素时,该集合将使用一种方法来确定它是否可以接受存储该元素(最终该元素尚不存在于集合中)。 它可以使用元素的 equals()hashCode()compareTo()

是否可以找到有关 Collection 的每个实现使用哪种方法的摘要?

非常感谢您的回答。

We find a lot of concrete subclasses under Collection.

While trying to add an element in a concrete collection, this collection will use a method to determine if it can accept to store the element (and eventually that this element is not already in the collection).
It could use equals(), hashCode() or compareTo() of the element.

Is it possible to find a summary about which method is used by each implementation of Collection ?

Thanks a lot for your answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向地狱狂奔 2024-09-09 11:33:04

首先,并非所有集合都会检查您要添加的元素是否已存在于集合中。例如,ArrayList 只是将元素追加到列表的尾部,而不检查它是否已经在列表中。如果保证集合只有对象的一份副本,则其他类使用 equals;如果元素应该是可比较的并且集合已排序(以找到插入它的正确位置),则使用compareTo。 Maps 也会使用 equals 来检查键,但是像 HashMap 这样的一些映射也会使用 hashCode() 来加速搜索过程(它们首先获取具有相同 hashcode 的所有键,然后对每个键使用 equals 来查找是否符合键已经存在并且已分配值,然后该值将被替换)。

但如果您想了解它们是如何工作的,您可以查看 jdk 中包含的源代码。
在eclipse中,我将JRE设置为安装jdk的文件夹,我可以使用CTRL + SHIFT + T打开jdk中的任何类(类型),CTRL +单击类的名称,或者对于方法,甚至CTRL+单击 ->打开实现,它会打开一个弹出窗口,其中包含实现该方法的类(如果它来自接口,或者直接在类中的方法,如果不是)

First of all not all collections check whether the element you're adding already exists in the collection. For example ArrayList just appends the element to the tail of the list, without checking if it's already in the list. Other classes use equals if the collection is guaranteed to have only one copy of the object, or compareTo, if the elements are supposed to be Comparable and the collection is sorted (to find the right place to insert it at). Maps will also use equals to check for the key, but some like HashMap will also use hashCode() to speed up the searching process (they first get all keys with the same hashcode, and then use equals on each of them to find if the key already exists and has a value assigned, which will then get replaced).

But if you want to see how they work you can check out the sources that are included with the jdk.
In eclipse I have the JRE set to the folder where the jdk is installed, and I can use CTRL+SHIFT+T to open any class (type) in the jdk, CTRL+click the name of a class, or for methods, even CTRL+click -> open implementation, which opens a pop up with the classes the implement that method (if it's from an interface, or the method directly in the class, if it's not)

我纯我任性 2024-09-09 11:33:04

任何具体的实现都应该在其 API 文档中指出它对其元素的行为做出了哪些假设。

通常,基于哈希的集合使用 hashCode(),基于树的集合使用 compareTo()Comparator,并且所有这些都使用 等于()。

Any concrete implementation should indicate in its API doc what assumptions it makes about the behaviour of its elements.

Generally, hash based collections use hashCode() and tree based ones use compareTo() or a Comparator, and all of them use equals().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文