Below is my class Emp and Class Mycomparator which implements Comparator. Both are in same package . I am getting error as The constructor TreeMap(Mycomparator) is undefined. I am new to Java. Please help.
public class Emp {
String name;
int eid;
public Emp(String name, int eid){
this.name=name;
this.eid = eid;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Emp e1 = new Emp("shankar",333);
Emp e2 = new Emp("Ronaldo",222);
Emp e3 = new Emp("Messi",111);
TreeMap<Integer,Emp> e = new TreeMap<Integer,Emp>(new Mycomparator());
e.put(1, e1);
e.put(2, e2);
e.put(3, e3);
Set<Integer> ss = e.keySet();
for(Integer i:ss) {
Emp ee = (Emp) e.get(i);
System.out.println(i+"--------"+ee.eid+"======="+ee.name);
}
}
}
Below is my class name Mycomparator which implements Comparator Interface.
public class Mycomparator implements Comparator<Entry<Integer,Emp>> {
@Override
public int compare(Entry<Integer,Emp> arg0, Entry<Integer,Emp> arg1) {
Map.Entry<Integer,Emp> obj1 = arg0;
Map.Entry<Integer,Emp> obj2 = arg1;
Emp e1 = (Emp) obj1.getValue();
Emp e2 =(Emp) obj2.getValue();
return e1.name.compareTo(e2.name);
}
}
}