Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is ultimately that the TreeSet will (being a set) only hold elements once, and it needs a way to know if it already contains an element, and it uses the Comparator you provided to do that. Since your comparator was using the enums for ordering, the Treeset saw two entries as equal if the enums matched.</p> <p>For details, see <a href="http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html</a>, particularly the paragraph</p> <blockquote> <p>Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface. </p> </blockquote> <p>Ultimately, if you want to use a TreeSet to sort on the enum, you'll need to expand the comparison logic to fall back on something else when the enums match. Something like</p> <pre><code>public int compare(Integer entity1, Integer entity2) { int returnValue = layers[entity1].compareTo(layers[entity2]); if(returnValue == 0){ returnValue = entity1.compareTo(entity2); } } </code></pre> <p>Which then makes the entities not match (and fail to add to the TreeSet) just because the enums match.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload