*
The moose likes Beginning Java and the fly likes TreeSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "TreeSet" Watch "TreeSet" New topic
Author

TreeSet

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305


Exception in thread "main" java.lang.ClassCastException: Animal cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(TreeMap.java:1188)
at java.util.TreeMap.put(TreeMap.java:531)
at java.util.TreeSet.add(TreeSet.java:255)
at MatualComparableTest.main(MatualComparableTest.java:15)

What's the problem here?


My one regret in life is that I am not someone else.
- Woody Allen
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

There is no natural ordering for your objects.
Lets say if i will keep 2,1,5,3 then natural ordering would be 1,2,3,5


So how does TreeSet will know ordering for your elements?
Hint : Implement comparable interface.
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

I modified some but still getting errors.


MatualComparableTest.java:12: error: constructor Animal in class Animal cannot be applied to given types;
Dog(String name){
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
MatualComparableTest.java:18: error: constructor Animal in class Animal cannot be applied to given types;
Cat(String name){
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
MatualComparableTest.java:38: error: cannot find symbol
return (this.name).compareTo(a.name);
^
symbol: variable name
MatualComparableTest.java:38: error: illegal start of type
return (this.name).compareTo(a.name);
^
Note: MatualComparableTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
4 errors
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

meeta, if you will look closely, you have missed constructors for super class.

hint : first line in constructor can be either this(0 or super().

either provide a default constructor for Animal(){} or call super(name).
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Also provide comparator for your object which you are going to keep inside Collection and not "MatualComparableTest".

Hope this will resolve your problem.
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

harshvardhan ojha wrote:meeta, if you will look closely, you have missed constructors for super class.

hint : first line in constructor can be either this(0 or super().

either provide a default constructor for Animal(){} or call super(name).


Thanks, now it's working.
 
wood burning stoves
 
subject: TreeSet
 
Similar Threads
question from Dan's exam - ClassCastException
invalid cast exception
Why can't we assign parent class object to child class reference?
Adding an element to a Treeset( was Plz clear My doubt ......)
Reg. casting from Khalid Mughal