我使用ConcurrentSkipListSet并使用contains方法.
根据JAVA doc for contains方法
如果此set包含指定的元素,则返回true.更正式地说,当且仅当此集合包含o.equals(e)的元素e时才返回true.
但根据我的测试,似乎不使用equals方法,而是比较器是强制性的.请帮助我理解JAVA规范和实现之间的这种异常
ConcurrentSkipListSet
/ **@H_403_12@ *如果使用比较器,则返回ComparableUsingComparator,否则@H_403_12@ *将密钥转换为Comparable,这可能会导致ClassCastException,@H_403_12@ *传播回调用者.@H_403_12@ * /@H_403_12@ 私人可比比较(对象键)
at java.util.concurrent.ConcurrentSkipListMap.comparable(ConcurrentSkipListMap.java:663)@H_403_12@ at java.util.concurrent.ConcurrentSkipListMap.doGet(ConcurrentSkipListMap.java:821)@H_403_12@ at java.util.concurrent.ConcurrentSkipListMap.containsKey(ConcurrentSkipListMap.java:1608)
我正在使用Oracle JDK 7
> ConcurrentSkipListSet是可导航的有序集合,因此所有元素必须保持自然顺序,或者必须指定比较器.@H_403_12@>我认为Javadoc声明不正确,或者至少它具有误导性.在引擎盖下,CSLS将委托给ConcurrentSkipListMap.containsKey,因此它现在不控制contains实现.也就是说,我认为可以有一个论点来澄清javadoc.
编辑:@H_403_12@对于这些对象无法比较的事实,还有一个投掷文档
ClassCastException – if the specified element cannot be compared with@H_403_12@ the elements currently in this set