aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Adding an element to a Treeset( was Plz clear My doubt ......) 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 "Adding an element to a Treeset( was Plz clear My doubt ......)" Watch "Adding an element to a Treeset( was Plz clear My doubt ......)" New topic
Author

Adding an element to a Treeset( was Plz clear My doubt ......)

kumaresan Natarajan
Greenhorn

Joined: Jul 23, 2005
Posts: 27


I am tring to compile this programe it throws classcastException...
Exception in thread "main" java.lang.ClassCastException
at java.util.TreeMap.compare(TreeMap.java:1085)
at java.util.TreeMap.put(TreeMap.java:463)
at java.util.TreeSet.add(TreeSet.java:209)
at SetTest.main(SetTest.java:6)
Press any key to continue . . .

Why its throws ClassCastException at Runtime...? Can anyone help me...

Thanks in advance
Kumaresan
[ July 28, 2006: Message edited by: Barry Gaunt ]

Thanks and Regards
kumaresan N
Stary Kapec
Ranch Hand

Joined: Dec 04, 2005
Posts: 81
Hi,

You use TreeSet. It is a sorted set. Elements you put in it are compared with eath other to be placed in the right order (default would be natural order). So they must implement Comparable, moreover they must also be mutually comparable.
Make the class Person comparable to solve the compilation problem.
[ July 28, 2006: Message edited by: Jasiek Motyka ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please, Kumaresan, use a meaningful topic title and use tags to surround formatted code.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
So, following on from Jasiek's post, you need a person class more or less like this:
Nishita Jain
Ranch Hand

Joined: Mar 30, 2006
Posts: 97
"u forgot to implement the Comparable interface "
Read this:
java.util.TreeSet.TreeSet()

Constructs a new, empty set, sorted according to the elements' natural order. All elements
inserted into the set must implement the Comparable interface. Furthermore, all such elements
must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any
elements e1 and e2 in the set. If the user attempts to add an element to the set that violates this
constraint (for example, the user attempts to add a string element to a set whose elements are
integers), the add(Object) call will throw a ClassCastException.

[ July 28, 2006: Message edited by: Nishita Jain ]
[ August 03, 2006: Message edited by: Nishita Jain ]

Nishita
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
hi, i modified your Person class and wrote like this now its running successfully.



Really? You can substitute your Person class for the original Person class and everything works?
[ July 28, 2006: Message edited by: Barry Gaunt ]
kumaresan Natarajan
Greenhorn

Joined: Jul 23, 2005
Posts: 27
Thanks Jasiek,Barry , Jain .....[ ]
Nishita Jain
Ranch Hand

Joined: Mar 30, 2006
Posts: 97
Hello,
i made changes in that code ya i agree that you cant replace that with original code...it was my mistake
actually the focus point was "u forgot to implement the Comparable interface "..

thanks
 
wood burning stoves
 
subject: Adding an element to a Treeset( was Plz clear My doubt ......)