I want a parallel set of string values ββsorted by length: longest β shortest.
This is my code (JAVA 8):
private ConcurrentSkipListSet<String> sortedSourceTypeNames = new ConcurrentSkipListSet<>(Comparator.comparing(String::length).reversed());
Here is the Java 8 documentation:
public ConcurrentSkipListSet(Comparator<? super E> comparator) {
m = new ConcurrentSkipListMap<E,Object>(comparator);
}
Now here's the weird thing:
- add new value "some_str" β ok
- add new value "some_els" β not added
- add new value "some" β good
While debugging this phenomenon, I saw that ConcurrentSkipListSet rejects new unique rows that have the same length of an existing row in the set.
And I was like Waaatt?!?!?
This is an unexpected behavior that is not mentioned in any documentation.
Is this a bug in the implementation of the JAVA ConcurrentSkipListSet? Or did I do something?
Edit:
!
, JAVA SortedSet ( ConcurrentSkipListSet):
* <p>Note that the ordering maintained by a sorted set (whether or not an
* explicit comparator is provided) must be <i>consistent with equals</i> if
* the sorted set is to correctly implement the <tt>Set</tt> interface. (See
* the <tt>Comparable</tt> interface or <tt>Comparator</tt> interface for a
* precise definition of <i>consistent with equals</i>.) This is so because
* the <tt>Set</tt> interface is defined in terms of the <tt>equals</tt>
* operation, but a sorted set performs all element comparisons using its
* <tt>compareTo</tt> (or <tt>compare</tt>) method, so two elements that are
* deemed equal by this method are, from the standpoint of the sorted set,
* equal. The behavior of a sorted set <i>is</i> well-defined even if its
* ordering is inconsistent with equals; it just fails to obey the general
* contract of the <tt>Set</tt> interface.