• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

TreeSet problem: not able to add two different instances to same set.

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was trying out the problem as follows:


and to my surprise this program only printed out one of the two instance's name.

I was expecting two entries in the Set. Can someone explain why is this happening?
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TreeSet uses the compareTo method to decide if two instances are equal (instead of equals). This has been discussed before like here...
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two cases with your question. Analyze that:


This will work as you expected:
 
Akanksha Mittal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit and Lukas.

But Lukas, I am not able to get the two entries even when I return positive value (>0) or negative value in compareTo.
I guess there is a problem with polling in the for loop. In the for loop which I have written,


Does h contain the last element remaining in the set or it contains the polled data?
I thought it was the polled data.

Having a tough time in setting up eclipse in my machine. It doesnt take the for(Object o: set) kind of declaration as given in the post mentioned above.
 
Ankit Garg
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akanksha Mittal wrote:
Does h contain the last element remaining in the set or it contains the polled data?
I thought it was the polled data.



look at the TreeSet.pollFirst() docs...
 
Ankit Garg
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akanksha Mittal wrote: I am not able to get the two entries even when I return positive value (>0) or negative value in compareTo.



I tried the code given by Lukas which returns -1 from compareTo method and I got two values as output. What's the complete code that you tried??
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akanksha Mittal wrote:Thanks Ankit and Lukas.

But Lukas, I am not able to get the two entries even when I return positive value (>0) or negative value in compareTo.
I guess there is a problem with polling in the for loop. In the for loop which I have written,


Does h contain the last element remaining in the set or it contains the polled data?
I thought it was the polled data.

Having a tough time in setting up eclipse in my machine. It doesnt take the for(Object o: set) kind of declaration as given in the post mentioned above.



The problem if with "for loop". I wrote that. Please, read cerefully.
Solution:
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The problem is with this method:

TreeSet uses this method to compare objects.
0 means that two objects are the same, and because of this when you try add object "one",
TreeSet doesn't add this object to the set because it thinks that this object is already in the set.

Look here: http://java.sun.com/j2se/1.4.2/docs/api/java/util/TreeSet.html

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 key comparisons using its compareTo (or compare) method, so two keys 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.



It means, that TreeSet uses 'Comparator.compare' or 'Comparable.compareTo' methods instead of 'Object.equals' to compare two objects.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the given code won't add anything if one object is added in the set. This set is indirectly made just a one object set. Lucas's code works fine. First you add "Hey" and then "Hi" and each time it says that this object is before other object(-1) that is why we get "Hi" and "Hey". Also use a test editor like notepad or Textpad to practise java programs rather than an IDE. It will be very helpful.
 
Akanksha Mittal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks to all of you. Finally I realised the problem.

1st problem was with the compareTo method which was returning 0 which was solved by replacing the 0 with positive or negative values.
But even after that there was a problem with the for loop where I was using the set.size() in the for loop & I didnt realize that it was changing in every iteration because of the pollFirst() method call.

Lukas you code solved the problem. Its running fine now.
 
Yes, my master! Here is the tiny ad you asked for:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic