• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Re: ClassCastException as I stored Employ objects into TreeSet..!

 
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am facing a problem while running this program at execution-time...but i did not understand why it is..?

here, in this program i am trying to observe the natural ordering of TreeSet....!

import java.util.*;
class Employ
{
private String firstName;
private String lastName;

public Employ(String firstName,String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}

}
public class TreeSetEmploy
{
public static void main(String...ar)
{
Set<Employ> setEmp=new TreeSet<>();

Employ e1 = new Employ("Tom", "Eagar");
Employ e2 = new Employ("Tom", "Smith");
Employ e3 = new Employ("Bill", "Joy");
Employ e4 = new Employ("Bill", "Gates");
Employ e5 = new Employ("Alice", "Wooden");

setEmp.add(e1);
setEmp.add(e2);

setEmp.add(e3);
setEmp.add(e4);
setEmp.add(e5);

System.out.println("Employees set is:"+setEmp);
}
}

let me know the error reason..?
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kiran madhan wrote:I am facing a problem  . . .



the error is:

Exception in thread "main" java.lang.ClassCastException: Employ cannot be cast to java.lang.Comparable
       at java.util.TreeMap.compare(Unknown Source)
       at java.util.TreeMap.put(Unknown Source)
       at java.util.TreeSet.add(Unknown Source)
       at TreeSetEmploy.main(TreeSetEmploy.java:26)

 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the API for TreeSet.


... a TreeSet instance performs all element comparisons using its compareTo (or compare) method



It needs something that is either Comparable (your Employ class would implement Comparable) or you need to provide it with a Comparator to use when it's constructed.

Since you say you want to use natural ordering then that implies Employ needs to implement Comparable.
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but when i stored strings..it ordered in alphabetical order...then why not it will do the same for this..!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kiran madhan wrote:but when i stored strings..it ordered in alphabetical order...then why not it will do the same for this..!



The String class implements Comparable, meaning it has a natural order. Your class does not, hence, you get a class cast exception. If you don't want to provide a natural order for your class, then you will need to create and use a Comparator.

Henry
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't quote the whole of a previous post, which siimply makes the thread longer and longer without adding any information.
lease don't write all in bold text.
I have removed some of those features, and also some empty tags.
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
class Employ  implements Comparable<Employ>
{
private String firstName;
private String lastName;

public Employ(String firstName,String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}
public int compareTo(String another)
{
int compValue=this.firstName.compareTo(another.firstName);

if(compValue==0)
{
return this.firstName.compareTo(another.firstName);
}
return compValue;
}

}
public class TreeSetEmploy
{
public static void main(String...ar)
{
Set<Employ> setEmp=new TreeSet<>();

Employ e1 = new Employ("Tom", "Eagar");
Employ e2 = new Employ("Tom", "Smith");
Employ e3 = new Employ("Bill", "Joy");
Employ e4 = new Employ("Bill", "Gates");
Employ e5 = new Employ("Alice", "Wooden");

setEmp.add(e1);
setEmp.add(e2);

setEmp.add(e3);
setEmp.add(e4);
setEmp.add(e5);

System.out.println("Employees set is:"+setEmp);
}
}


i tried this...but still i got below errors...may i know how to resolve them..!

the errors are:

TreeSetEmploy.java:2: error: Employ is not abstract and does not override abstra
ct method compareTo(Employ) in Comparable
       class Employ  implements Comparable<Employ>
       ^
TreeSetEmploy.java:14: error: cannot find symbol
       int compValue=this.firstName.compareTo(another.firstName);
                                                     ^
 symbol:   variable firstName
 location: variable another of type String
TreeSetEmploy.java:18: error: cannot find symbol
       return this.firstName.compareTo(another.firstName);
                                              ^
 symbol:   variable firstName
 location: variable another of type String
3 errors

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kiran madhan wrote:
i tried this...but still i got below errors...may i know how to resolve them..!

the errors are:

TreeSetEmploy.java:2: error: Employ is not abstract and does not override abstract method compareTo(Employ) in Comparable
       class Employ  implements Comparable<Employ>
       ^



The error message is pretty clear -- you didn't implement the compareTo(Employ) method as defined in the interface.

Henry
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot...! I understand my mistake..!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags (← that's a link)

Your compareTo method parameter is a String, which doesn't have those fields that you try to reference on another. See Henry's reply.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic