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
The ConcurrentSkipListSet class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractSet class. It provides a scalable and concurrent version of NavigableSet in Java. The implementation of ConcurrentSkipListSet is based on ConcurrentSkipListMap.
The ConcurrentSkipListMap is a scalable implementation of ConcurrentNavigableMap. All the elements are sorted based on natural ordering or by the Comparator passed during it's construction time.
The iterator() method of java. util. concurrent. ConcurrentSkipListSet is an in-built function in Java which is used to return an iterator over the elements in this set in ascending order.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With