ConcurrentSkipListSet using Comparator will not add new unique values

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:

    /**
     * Constructs a new, empty set that orders its elements according to
     * the specified comparator.
     *
     * @param comparator the comparator that will be used to order this set.
     *        If {@code null}, the {@linkplain Comparable natural
     *        ordering} of the elements will be used.
     */
    public ConcurrentSkipListSet(Comparator<? super E> comparator) {
        m = new ConcurrentSkipListMap<E,Object>(comparator);
    }

Now here's the weird thing:

  1. add new value "some_str" β†’ ok
  2. add new value "some_els" β†’ not added
  3. 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.
+5
1

, , , , , .

ConcurrentSkipListSet

Set<String> set = new ConcurrentSkipListSet<>(
        Comparator.comparing(s -> s));

Set<String> set = new ConcurrentSkipListSet<>(
        Comparator.naturalOrder());

, , , , .

- , .

Set<String> set = new ConcurrentSkipListSet<>(
        Comparator.comparing(String::length).reversed()
        .thenComparing(s -> s));

set.add("aa");
set.add("bb");
set.add("aaa");
set.add("ccc");
System.out.println(set);

[aaa, ccc, aa, bb]
+3

Source: https://habr.com/ru/post/1691754/


All Articles