2011-11-20 82 views
1
public int compareTo(Object o) { 
    Doctor d = (Doctor)o; 
    return (doctor_name.compareTo(d.doctor_name)); 
} 

这是我在医生级别可比TreeMap中不排序

jTextArea.setText(""); 
int doctor_id = Integer.parseInt(jTextField2.getText()); 
String doctor_name = jTextField3.getText(); 
String hospitalName = jTextField4.getText(); 

Doctor d = new Doctor(doctor_id,doctor_name,hospitalName); 
hmDoctor.put(doctor_id, d); 

这是我的HashMap

,这是我的树地图

TreeMap<Integer,Doctor> tmDoctor = new TreMap<Integer,Doctor>(hmDoctor); 
jTextArea.setText(""); 
Set keys = tmDoctor.keySet(); 
Iterator<Integer> ite = keys.iterator(); 

while(ite.hasNext()) 
{ 
    int doctorID = ite.next(); 
    Doctor d = tmDoctor.get(doctorID); 
    tmDoctor.put(doctorID, d); 
    jTextArea1.append(d.toString()); 
} 

它不分类医生的名字。为什么?

回答

10

TreeMap根据它们的键而不是值对元素进行排序。 作为一种替代方法,您可以使用由医生姓名索引的TreeMap<String, Doctor>。 或者,根据你需要做什么,你可以让医生在TreeSet

+1

我可以做到这一点与由doctorName设置的树不是吗? – snnlankrdsm

+0

如果你使用集合,它将是一个有序的医生集合(你可能必须重写'equals()'),这将允许测试,如'yourSet.contains(d)'。如果您按照名称使用地图,则可以使用'yourMap.get(“Gregory House”)'来获取'Doctor'对象。 – Vlad

+0

private TreeMap tmDoctor = new TreeMap ();如果我这样做,它现在的作品,但什么是医生类可比较的意义? – snnlankrdsm