java - ClassCastException when Treeset.add(), despite implementing comparable with compareTo method -


i have class contact have displayed below. want each contact object have list of other contacts. chose treeset because i'd avoid duplicate contacts in same list. contact class implements comparable , has constructed compareto() method compares instance string variables. understand compareto method used when adding treeset because added elements sorted.

when try add contact object contact[] (see setcontacts() method below) treeset, receive classcastexception. system.out messages is:

exception in thread "main" java.lang.classcastexception: shared.contact cannot cast java.lang.string @ java.text.collator.compare(unknown source) @ java.util.treemap.compare(unknown source) @ java.util.treemap.put(unknown source) @ java.util.treeset.add(unknown source) @ shared.contact.setcontacts(contact.java:51) <-- right here treeset.add() @ server.server.readincontacts(server.java:191) @ server.server.main(server.java:51) 

what not understand in of classes have built try convert string. below entire contact class.

does see wrong or see why exception occuring?

i'd happy post more code if tells me else might relevant. don't know else important see. in advance.

package shared;  import java.io.objectinputstream; import java.io.objectoutputstream; import java.io.serializable; import java.text.collator; import java.util.linkedlist; import java.util.treeset;  public class contact implements serializable, comparable<contact>{      private static final long serialversionuid = 1l;     private string name;     private boolean online;     private treeset<contact> contacts;     private objectoutputstream oos;     private objectinputstream ois;       public contact(string name){         this(name, null, null);     }     public contact (string name, objectinputstream ois, objectoutputstream oos){         this.name = name;         this.ois = ois;         this.oos = oos;         contacts = new treeset<contact>(collator.getinstance());     }     public string getname(){         return name;     }     public void login(objectinputstream ois, objectoutputstream oos){         this.ois = ois;         this.oos = oos;     }     public objectinputstream getobjectinputstream(){         return ois;     }     public objectoutputstream getobjectoutputstream(){         return oos;     }     public void setonlinestatus(boolean status){         online = status;     }     public boolean isonline(){         return online;     }     public void setcontacts(contact[] contacts){         this.contacts.clear();         (contact c: contacts){             this.contacts.add(c);  <-- right here exception occurs         }     }      @override     public int compareto(contact c){         return this.name.compareto(c.getname());     }      public boolean equals(object o){         contact c = (contact)o;         return this.name.equals(c.getname());     }      public void addcontact(contact c){         linkedlist<contact> list = new linkedlist<contact>();         (contact contact: contacts){             list.add(contact);         }     }     public contact[] getcontacts(){         return contacts.toarray(new contact[0]);     }          public int hashcode(){         return name.hashcode();     }      public string tostring(){         return this.name;     }     } 

the answer right here in collator.java:

public int compare(object o1, object o2) { return compare((string)o1, (string)o2); } 

it's not obvious me why on earth passing in own collator? dealing non ascii?

you have inconsistency here: compareto() == 0 should same equals() since in both cases thing seem care 'name'

so, on 1 hand state want remove identical entries , use comparative identity use collator can produce inconsistent results equals.

if want remove redundancy skip collator , either use hashset or treeset no collator

alternatively if must use collated names use

treemap<string,contact> contacts = new treemap<string,contact>(collator.getinstance()); contacts.put(contact.getname(),contact); 

you can use:

collection<contact> uniquecontacts = contacts.values(); 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -