为什么ConcurrentSkipListSet.contains需要比较,而不是平等 [英] Why ConcurrentSkipListSet.contains requires comparator and not equals

查看:125
本文介绍了为什么ConcurrentSkipListSet.contains需要比较,而不是平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ConcurrentSkipListSet并使用contains方法。



根据JAVA doc for contains方法



如果此set包含指定的元素,则返回true。更正式地说,当且仅当此集包含元素e以使得o.equals(e)时才返回true。



但是根据我的测试,似乎是equals方法不使用,而是比较器是强制性的。请帮助我理解JAVA规范和实现之间的这种异常



ConcurrentSkipListSet



/ **
*如果使用比较器,则返回ComparableUsingComparator, else
* cast key作为Comparable,这可能导致ClassCastException,

*传播回调用者。
* /
private可比较(对象键)



at java.util.concurrent.ConcurrentSkipListMap.comparable(ConcurrentSkipListMap.java:663)
at java.util.concurrent.ConcurrentSkipListMap.doGet(ConcurrentSkipListMap.java:821)
at java.util.concurrent.ConcurrentSkipListMap.containsKey(ConcurrentSkipListMap.java:1608)
$ b

我正在使用Oracle JDK 7

解决方案

我认为有两个问题/关注点,(1)为什么包含是否需要比较器可比较。 (2)Javadoc说它将使用等于方法。


  1. ConcurrentSkipListSet是一个可导航的有序集合,因此所有元素必须保持自然顺序,或者您必须指定比较器。

  2. 我认为Javadoc声明不正确,或者至少它具有误导性。在引擎盖下,CSLS将委托给ConcurrentSkipListMap.containsKey,因此它现在不控制contains实现。也就是说,我认为可以有一个论点来澄清javadoc。

编辑:
还有一个 throws doc这些对象无法比较的事实


ClassCastException - 如果指定的元素无法与
比较此套装中的元素



I am using ConcurrentSkipListSet and using contains method.

As per JAVA doc for contains method

Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that o.equals(e).

But as per my test , it appears that equals method is not used but rather Comparator is mandatory. Please help me understand this anomaly between JAVA spec and implementation

ConcurrentSkipListSet

/** * If using comparator, return a ComparableUsingComparator, else * cast key as Comparable, which may cause ClassCastException, * which is propagated back to caller. */ private Comparable comparable(Object key)

at java.util.concurrent.ConcurrentSkipListMap.comparable(ConcurrentSkipListMap.java:663) at java.util.concurrent.ConcurrentSkipListMap.doGet(ConcurrentSkipListMap.java:821) at java.util.concurrent.ConcurrentSkipListMap.containsKey(ConcurrentSkipListMap.java:1608)

I am using Oracle JDK 7

解决方案

I think there are two questions/concerns, (1) Why does the contains require a Comparator or Comparable. (2) The Javadoc says it will use the equals method.

  1. A ConcurrentSkipListSet is a navigable ordered collection so all elements either must maintain a natural order, or you must specify a comparator.
  2. I think the Javadoc is stated incorrectly, or at the very least it is misleading. Under the hood, the CSLS will delegate to a ConcurrentSkipListMap.containsKey so it, right now, does not control the contains implementation. That said, I think there can be an argument to clarify the javadocs.

EDIT: There is also a throws doc for the fact these objects aren't comparable

ClassCastException - if the specified element cannot be compared with the elements currently in this set

这篇关于为什么ConcurrentSkipListSet.contains需要比较,而不是平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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