Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeSet

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 157
1
Android Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic