简体   繁体   English

Java Set的唯一对象标识

[英]Unique object identification of Java Set

In Java, a Set cannot contain two unique objects where as a List doesn't have that restriction. 在Java中,Set不能包含两个唯一的对象,因为List没有该限制。 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. 最有可能是equals方法或hashCode方法的实现,或正在添加的两个对象的实现。 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 ? 是equals方法,hashcode方法还是这两种方法或其他方法?

It depends on the Set implementation. 这取决于Set实现。 For HashSet and LinkedHashSet , it uses both equals and hashCode methods. 对于HashSetLinkedHashSet ,它同时使用equalshashCode方法。 For TreeSet , it uses the natural Comparator of the object or a specific provided Comparator . 对于TreeSet ,它使用对象的自然Comparator或提供的特定Comparator

Basically, the Set javadoc explains this (emphasis mine): 基本上, Set javadoc对此进行了解释(强调我的意思):

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 . 更正式地说, 集合不包含元素对e1和e2,使得e1.equals(e2)最多包含一个null元素 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 . 除了从Collection接口继承的规定外 ,Set接口对所有构造函数的协定以及add, equals和hashCode方法的协定附加了其他规定

TreeSet has another behavior since it implements SortedSet (this interface extends the Set interface). TreeSet具有另一个行为,因为它实现了SortedSet (此接口扩展了Set接口)。 From its javadoc (emphasis mine): 从其javadoc(重点是我的):

A Set that further provides a total ordering on its elements. 一个Set,进一步提供其元素的总体排序。 The elements are ordered using their natural ordering, or by a Comparator typically provided at sorted set creation time. 元素使用其自然顺序或通过通常在排序集创建时提供的Comparator进行排序。 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. 请注意,如果排序集要正确实现Set接口,则排序集(无论是否提供显式比较器)所维护的顺序必须与equals一致(有关与以下内容一致的精确定义,请参见Comparable接口或Comparator接口)。之所以这样,是因为Set接口是根据equals操作定义的,但是排序后的set使用其compareTo(或compare)方法执行所有元素比较。

HashSet Source: seems the elements are stored as keys in a HashMap (which requires unique keys) and the put method in HashMap checks the following: HashSet源:似乎元素作为键存储在HashMap (需要唯一键),并且HashMapput方法检查以下内容:

if (e.hash == hash && ((k = e.key) == key || key.equals(k)))

So it compares both the hash and runs equals . 因此,它同时比较哈希值和运行equals

TreeSet uses a TreeMap to back its data and TreeMaps put uses a comparator. TreeSet中使用树状图中备份其数据和树状图put采用了比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM