Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in treeset...

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SOURCE:www.javabeat.net

import java.util.*;
class Test12
{
public static void main(String[] a)
{
Set s=new TreeSet();
s.add(new Person(10));
s.add(new Person(20));
System.out.println(s);
}
}
class Person
{
Person(int i)
{
}
}


why it is giving compilation error.............>?

 
Henry Wong
author
Marshal
Pie
Posts: 21200
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why it is giving compilation error.............>?



What compilation error are you getting?

Henry
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh are you getting a compilation warning or error???
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just ran the code and havenot got any compilation errors...
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The problem is that Person doesn't implement the java.lang.Comparable interface. Implement it and you wont have exceptions just warnings
 
Henry Wong
author
Marshal
Pie
Posts: 21200
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that Person doesn't implement the java.lang.Comparable interface. Implement it and you wont have exceptions just warnings


Or implement an comparator, that is passed to the tree set in the constructor. Either option will work. The tree set will use the comparator first, and if not available will assume the objects are comparable.

Henry
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but treeset can sort it automatically
which method it can use for sorting?

why should i implement Comparable interface?






 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ganeshkumar,
I am also having the same doubt..It would be great if anyone explain this...


Thanks
Preetha
 
Henry Wong
author
Marshal
Pie
Posts: 21200
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ganeshkumar cheekati:
but treeset can sort it automatically
which method it can use for sorting?

why should i implement Comparable interface?



What do you mean by "treeset can sort it automatically"?

A treeset can sort strings. A treeset can sort integer objects. A treeset can sort characters objects. Etc.... And the reason a treeset can sort them, is because all of these class types implement the comparable interface.

It uses the comparable interface (as one of the ways) to help sort the items. If you don't implement it (and also don't provide a comparator), it won't be able to sort it.

Henry
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes henry....

i know that wrapper,string,Date are implemented the Comparable interface thats why we need not do that.

but here in my program how can i implement comparable interface?



is it correct?

it is giving me compiler error as:

Person is not abstarct and does not override abstarct method compareTo(java.lang.Object) in java.lang.Compaarable...

can you show me how to write method in Person class?
 
Vipun Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be an answer. One of the possibilities.




as integers do extend comparable interface.
The class can be sorted on the integer value.. passed during intialization.
Assigning it to the instance variable and using the instance variable to sort the Person class.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vipun,

How could it be for parameterless contructor?
like t.add(new Person()); if Person is a type of Set created.

import java.util.*;
public class TestSC {


public static void main(String args[]){
TreeSet<TestSC> at=new TreeSet<TestSC>();
at.add(new TestSC());
at.add(new TestSC());
at.add(new TestSC());

System.out.println(at.first());
}
}


thanks
Preetha
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh
I have modified your program a bit to properly implement Comparable.Here it is..

 
Vipun Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Preetha,

hey its not matter of parameterless constructor or something else.

What you need is a method or characteristic on which you sort the things.

Suppose you have 100 persons. you cannot sort them with out knowing anything about their behaviour or something else, thats unique to that individual.

All that matters is how to sort and that can be done implementing java.util.Comparable interface and implementing the

public int toCompare(T o) method. Where T can be the class which you are implementing.

Hope you got the idea behind implementing Comparable interface ..!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic