Results 1 to 8 of 8
  1. #1
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default why this ClassCastExceptioni????

    In following code why it gives class cast exception??? If i comment insesrting second person object then it is ok... What is the reason ..!

    Thank you..


    import java.util.*;
    public class Testy{
    public static void main(String a[]){
    Set s = new TreeSet();
    s.add(new Person(20));
    s.add(new Person(10)); //If i comment this then OK

    System.out.println(s);
    }
    }
    class Person{

    int x;
    Person(int i){



    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    What does the compareTo method of your Person class look like?

  3. #3
    chathura87 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    There is not compareTo method for this...
    BUT i can still add element 1... but not 2..

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    First of all, post code in tags to make it more readable.

    Read the API for Set
    some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException.

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

    You need to have your Person class implement Comparable.
    Java Code:
    import java.util.Set;
    import java.util.TreeSet;
    
    public class Testy {
    
      public static void main(String a[]) {
        Set s = new TreeSet();
        s.add(new Person(20));
        s.add(new Person(10));
    
        System.out.println(s);
      }
    }
    
    class Person implements Comparable {
    
      int x;
    
      Person(int i) {
        x = i;
      }
    
      public int compareTo(Object o) {
        return x - ((Person) o).x;
      }
    }
    Better than that, use Generics, that's what it's meant for.
    Java Code:
    import java.util.Set;
    import java.util.TreeSet;
    
    public class Testy {
    
      public static void main(String a[]) {
        Set s = new TreeSet<Person>();
        s.add(new Person(20));
        s.add(new Person(10));
    
        System.out.println(s);
      }
    }
    
    class Person implements Comparable<Person> {
    
      int x;
    
      Person(int i) {
        x = i;
      }
    
      public int compareTo(Person o) {
        return x - o.x;
      }
    }
    db

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by chathura87 View Post
    There is not compareTo method for this...
    BUT i can still add element 1... but not 2..
    That's what I thought. Read DBs comments above which are essentially saying, RTFM, and I will add to that, that you should know at least something about the structures you are using, at least enough to know how to use them.
    Last edited by masijade; 03-01-2011 at 11:16 AM. Reason: Typo "will" not "wall" ;-)

  6. #6
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    All sorted structures (like your TreeSet) would require a default sort behaviour. That's why you need the compareTo() method implemented.

  7. #7
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    TreeSet's job is to sort data. If you were adding some Integer or String type it'd have been fine and there is already a mechanism to sort. But this object is your own object and TreeSet doesn't know how to sort it. So you have to implement Comparable

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,381
    Blog Entries
    7
    Rep Power
    20

    Default

    A TreeSet needs to compare objects a and b where a < b or a > b or a == b. It either does that by using a Comparator or the objects to be stored in the set have to implement the Comparable<T> interface where T is the type of the objects to be stored. Your Person class doesn't implement that interface nor did you supply a Comparator implementation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •