• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in TreeSet

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the Output? and why?
import java.util.*;
public class Collection
{
public static void main(String arg[])
{
TreeSet s=new TreeSet();
s.add(new Sample());
s.add(new Sample());
for(Object o:s)
System.out.println(o);
}
}
class Sample implements Comparable
{
int a;
int b;
public int compareTo(Object o)
{
return a-b;
}
}
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all tell me..What is your understanding of code and where is the confusion..!!!

following research might help you:

1) How Set is different form List ?

2) What is the return type of Set.add( ) method..and what are different output condition..and how those condition is evaluated ?
 
GopiKrishnan Chelliah
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In List we can add list of objects. even duplicates
but in Set we cannot add duplicate objects.
my question was how the compare method works.
It gives output only one object why?
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you add an object(obj2)..it calls..

obj2.compare( already present object in the set)

in your case

obj2.compare(obj1)

if it returns 0, then obj2 will not be added..!!

This is what i think..!!
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your sample class each of your integer variables are defaulting to 0. Also the compareTo method is only looking at the a &b variables of the current instance. This is returning a 0 which is telling the set that the objects being compared are equal.
 
I don't like that guy. The tiny ad agrees with me.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic