• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

TreeSet with compartor constructor

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks, I'm having a play again. I have the following code



So I've defined my own order based upon string length, the above gives the following output

First
Second

Where's Third gone? Is it because Third and First have the same length? Therefore one overwrites the other?
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
of course your comparator is used as an equals() method, so if you return a zero it means that they are equal, and since you are using a SET which dosen't allow duplicates only one can exist.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can modify the code to
>
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The constructor you used for TreeSet is new TreeSet<String>(comp), in Java API it says:

public TreeSet(Comparator<? super E> comparator)

Constructs a new, empty tree set, sorted according to the specified comparator.



And for the Comparator:

The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in S.



Since TreeSet implements SortSet, and in API SortSet notes:

Note that the ordering maintained by a sorted set (whether or not an explicit comparator is provided) must be consistent with equals if the sorted set is to correctly implement the Set interface. (See the Comparable interface or Comparator interface 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 sorted set 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 sorted set, equal. The behavior of a sorted set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.



When trying to add "Third" to the TreeSet, it compares with existing element "First" and returns 0 and won't be able to sort the set, so it failed.

I have a question though, the TreeSet's add method says:

public boolean add(E e)

Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.


It does not mention sort or compare stuff, so it seems as long as the new element fulfil the condition (e==null ? e2==null : e.equals(e2)), it should be able to be added. It seems contradictory on this example?



result:
First=Third? false
Add First: true
Add Second: true
Add Third: false
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread is a bit old, but there's not really a good answer, so I'll post this.


The previous post is correct in stating that if 2 items, when compared in a TreeSet, return 0, the the item doesn't get added. So, if they are equal, I ran the regular String compare to order alphabetically.
 
Marshal
Posts: 78657
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please remember that you have to tailor an exam answer to the version of Java® current at the time; ten years ago we used Java6. Nowadays most people would write a λ maybe using some of the methods introduced in 2014 (Java8). I would do something like this:-Jia Tan has told us why that Comparator is permissible only in a cert exam revision book. Did anybody notice that return 1/-1; is wrong. That is why you are getting strange behaviour wherebydoesn't baheve the same as...and the reason the TreeSet#add() method doesn't say anything about consistency with equals() is that  the top of the documentation page says this:-

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.

I presume that hasn't changed since Java6.
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic