This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
kumaresan Natarajan
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
Stary Kapec
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, Kumaresan, use a meaningful topic title and use tags to surround formatted code.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, following on from Jasiek's post, you need a person class more or less like this:
 
Nishita Jain
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jasiek,Barry , Jain .....[ ]
 
Nishita Jain
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic