public class BigD{
public static void main(String[] args) {
List<Employee> emps = new ArrayList<Employee>();
emps.add(new Employee("Bal", "5"));
emps.add(new Employee("Kiri", "7"));
emps.add(new Employee("Pad", "2"));
Map tree = new TreeMap();
for(int i=0; i<emps.size(); i++) {
Employee emp = null;
emp = emps.get(i);
System.out.println("hashcode : " + emp.hashCode());
tree.put(emp, emp.getFirstNM()); // why is not keeping all three elements here ?
}
System.out.println(tree.size()); //why does it print the size as "1"
}
}
class Employee implements Comparable {
private String firstNM;
private String lastNM;
Employee(String firstNM, String lastNM) {
this.firstNM = firstNM;
this.lastNM = lastNM;
}
public String getFirstNM() {
return firstNM;
}
public void setFirstNM(String firstNM) {
this.firstNM = firstNM;
}
public String getLastNM() {
return lastNM;
}
public void setLastNM(String lastNM) {
this.lastNM = lastNM;
}
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}
}
请让我知道为什么树形图“树”只有一个元素是员工对象“Pad”,即使我添加的所有三个员工对象都具有不同的哈希码。是因为我没有覆盖等于/哈希码吗?如果是 - 当所有人都返回不同的哈希码时,我为什么要覆盖您的想法将不胜感激。谢谢