• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

doubt in treeset...

 
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.............>?

 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh are you getting a compilation warning or error???
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just ran the code and havenot got any compilation errors...
 
Bartender
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
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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?






 
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
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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?
 
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
 
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 ..!!
    Bookmark Topic Watch Topic
  • New Topic