TreeMap不排序

snn*_*dsm 1 java treemap

public int compareTo(Object o) {
    Doctor d = (Doctor)o;
    return (doctor_name.compareTo(d.doctor_name));
}
Run Code Online (Sandbox Code Playgroud)

这是我在博士课上的可比性

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);
Run Code Online (Sandbox Code Playgroud)

这是我的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());
}
Run Code Online (Sandbox Code Playgroud)

它没有对医生姓名进行排序.为什么?

Vla*_*lad 10

TreeMap根据自己的钥匙,而不是值各种因素.作为替代方案,您可以使用TreeMap<String, Doctor>医生姓名索引.或者,根据您的需要,您可以让医生保持健康TreeSet.