Guava JavaDocs 中的集合基于不同的 "equivalence relations" 是什么意思?
What do the Guava JavaDocs mean by sets being based on different "equivalence relations"?
番石榴 JavaDocs Sets.SetView.union()
(以及 intersection()
、difference()
和 symmetricDifference()
)提及 "equivalence relations":
Results are undefined if set1
and set2
are sets based on different equivalence relations (as HashSet
, TreeSet
, and the Map.keySet()
of an IdentityHashMap
all are).
我很难理解那句话的意思。
词汇表defines "equivalence relation" as reflexive ("a.relation(a)
is always true
"), symmetric (a1.relation(a2) == a2.relation(a1)
) and transitive (a1.relation(a2) && a2.relation(a3)
implies a1.relation(a3)
) - and refers to Object.equals()
' docs. (Unfortunately, the Guava wiki没有详细说明...
但是 Set
的不同类型在这方面有何不同(即等价关系)?好像都继承了equals()
from AbstractSet
?它与集合包含的对象类型无关(例如 Set<Cow>
与 Set<Chicken>
),对吗?
听起来他们指的是 Set
出于某种原因不使用 equals
和 hashCode
来比较元素。最常见的示例是带有自定义 Comparator
的 TreeSet
。例如,我们可以有这样的东西:
Set<String> a = new TreeSet<>();
Set<String> b = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
a
和b
的并集、交集等未定义,因为a
和b
定义的元素之间的等价关系不同
Java SE在讲到与equals
不一致的顺序时也提到了这种情况(见TreeSet
):
Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set
interface. (See Comparable
or Comparator
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 TreeSet
instance performs all element comparisons using its compareTo
(or compare
) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set
interface.
番石榴 JavaDocs Sets.SetView.union()
(以及 intersection()
、difference()
和 symmetricDifference()
)提及 "equivalence relations":
Results are undefined if
set1
andset2
are sets based on different equivalence relations (asHashSet
,TreeSet
, and theMap.keySet()
of anIdentityHashMap
all are).
我很难理解那句话的意思。
词汇表defines "equivalence relation" as reflexive ("a.relation(a)
is always true
"), symmetric (a1.relation(a2) == a2.relation(a1)
) and transitive (a1.relation(a2) && a2.relation(a3)
implies a1.relation(a3)
) - and refers to Object.equals()
' docs. (Unfortunately, the Guava wiki没有详细说明...
但是 Set
的不同类型在这方面有何不同(即等价关系)?好像都继承了equals()
from AbstractSet
?它与集合包含的对象类型无关(例如 Set<Cow>
与 Set<Chicken>
),对吗?
听起来他们指的是 Set
出于某种原因不使用 equals
和 hashCode
来比较元素。最常见的示例是带有自定义 Comparator
的 TreeSet
。例如,我们可以有这样的东西:
Set<String> a = new TreeSet<>();
Set<String> b = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
a
和b
的并集、交集等未定义,因为a
和b
定义的元素之间的等价关系不同
Java SE在讲到与equals
不一致的顺序时也提到了这种情况(见TreeSet
):
Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the
Set
interface. (SeeComparable
orComparator
for a precise definition of consistent with equals.) This is so because theSet
interface is defined in terms of theequals
operation, but aTreeSet
instance performs all element comparisons using itscompareTo
(orcompare
) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of theSet
interface.