Java集合的唯一对象标识 [英] Unique object identification of Java Set

查看:646
本文介绍了Java集合的唯一对象标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,集合不能包含两个唯一对象,因为列表没有此限制。在集合中标识唯一对象的机制是什么。最可能的是,它可能是equals方法或hashCode方法的实现或两个正在添加的对象。有没有人知道什么是确定唯一对象的实际机制。是equals方法,hashcode方法还是这两种方法或别的什么?

解决方案

这取决于 Set 实现。对于 HashSet LinkedHashSet ,它使用 equals code> hashCode 方法。对于 TreeSet ,它使用对象的自然 Comparator 或特定的 Comparator



基本上, 设置 javadoc 解释了这一点(强调我的):



< blockquote>

不包含重复元素的集合。更正式地,集合不包含元素e1和e2对,使得e1.equals(e2)和至多一个空元素

Set接口添加了额外的规定,除了那些继承自Collection接口的接口之外,所有构造函数的合约以及add, equals和hashCode方法的合约。


TreeSet 有另一种行为,因为它实现了 SortedSet (此界面扩展了 Set 界面)。从它的javadoc(强调我的):


A进一步提供其元素的总排序。元素使用其自然排序或通过在排序集创建时间通常提供的比较器来排序。集合的迭代器将以升序元素顺序遍历集合。)



请注意,由排序集合(不管是否提供显式比较器)维护的排序必须是一致的如果排序集合要正确实现Set接口(请参阅Comparable接口或Comparator接口以获得与equals一致的精确定义)。这是因为 Set接口是根据equals操作定义的,但是排序集合使用compareTo(或compare)方法执行所有元素的比较。



In Java, a Set cannot contain two unique objects where as a List doesn't have that restriction. What is the mechanism in a Set which identifies unique objects. Most probably it might be the implementation of the equals method or hashCode method or both of the objects which are being added. Does anyone know what's the actually mechanism of identifying unique objects. Is it the equals method, hashcode method or both the methods or something else ?

解决方案

It depends on the Set implementation. For HashSet and LinkedHashSet, it uses both equals and hashCode methods. For TreeSet, it uses the natural Comparator of the object or a specific provided Comparator.

Basically, the Set javadoc explains this (emphasis mine):

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

The Set interface places additional stipulations, beyond those inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add, equals and hashCode methods.

TreeSet has another behavior since it implements SortedSet (this interface extends the Set interface). From its javadoc (emphasis mine):

A Set that further provides a total ordering on its elements. The elements are ordered using their natural ordering, or by a Comparator typically provided at sorted set creation time. The set's iterator will traverse the set in ascending element order.)

Note that the ordering maintained by a sorted set (whether or not an explicit comparator is provided) must be consistent with equals if the sorted set is to correctly implement the Set interface (See the Comparable interface or Comparator interface for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a sorted set performs all element comparisons using its compareTo (or compare) method.

这篇关于Java集合的唯一对象标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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