java - I am trying to short Employee according to firstname but I am getting this error? -


i getting error exception in thread "main" java.lang.classcastexception : com.genous.employee cannot cast java.lang.comparable

at java.util.arrays.mergesort(arrays.java:1157)

at java.util.arrays.sort(arrays.java:1092)

at java.util.collections.sort(collections.java:134)

at com.genious.employee.main(employee.java:54)//

public class employee implements comparator { string firstname; string lastname; int mobileno; public employee(string firstname,string lastname,int mobileno) { this.firstname=firstname; this.lastname=lastname; this.mobileno=mobileno; }  @override public string tostring() {         return firstname; }   @override public int compare(object o1, object o2) { employee e2=(employee)o1; employee e3=(employee)o2; int i=e2.firstname.compareto(e3.firstname); if(i!=0)     return i;  return i; }   /**  * @param args  */ public static void main(string[] args) { arraylist list=new arraylist();     employee e=new employee("anand","pandey",93456666);     employee e1=new employee("sheel","nidhi",678956344);     employee e5=new employee("shumit", "kumar", 97390267);     employee e6=new employee("kamal", "kumar", 97390267);     list.add(e);     list.add(e1);     list.add(e5);     list.add(e6);     system.out.println(list); collections.sort(list);     system.out.println(list); }    } 

you can override equals() method in employee class , add employee objects set instead of list list of unique employee objects.and if want count of duplicate entries can subtract set size() list size()

          //provided have overriden equals() method           list<employee> employeelist = new arraylist<employee>();           set<employee> employeeset = new hashset<employee>(employeelist);            int dupentries = employeelist.size() - employeeset.size();           system.out.println("dup entries : "+dupentries); 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -