aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in treeset... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in treeset..." Watch "doubt in treeset..." New topic
Author

doubt in treeset...

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
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.............>?



SCJP5 and SCWCD1.5
Think Twice Act Wise...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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



What compilation error are you getting?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Ganesh are you getting a compilation warning or error???


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

I just ran the code and havenot got any compilation errors...


-kolli
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700



The problem is that Person doesn't implement the java.lang.Comparable interface. Implement it and you wont have exceptions just warnings


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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

Joined: Oct 13, 2008
Posts: 362
but treeset can sort it automatically
which method it can use for sorting?

why should i implement Comparable interface?






Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi Ganeshkumar,
I am also having the same doubt..It would be great if anyone explain this...


Thanks
Preetha
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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

Joined: Oct 13, 2008
Posts: 362
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

Joined: Mar 19, 2008
Posts: 23
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.


Vipun Reddy
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
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

Joined: Sep 13, 2008
Posts: 178
Ganesh
I have modified your program a bit to properly implement Comparable.Here it is..

Vipun Reddy
Greenhorn

Joined: Mar 19, 2008
Posts: 23
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 ..!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in treeset...