Reputation: 1401
public int compareTo(Object o) {
Doctor d = (Doctor)o;
return (doctor_name.compareTo(d.doctor_name));
}
This is my comparable in Doctor class
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);
This is my hashmap
and this is my tree map
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());
}
It doesn't sort doctor names. Why?
Upvotes: 1
Views: 413
Reputation: 18633
The TreeMap
sorts elements according to their keys, not values.
As an alternative, you could use a TreeMap<String, Doctor>
indexed by the doctor's name.
Or, depending on what you need to do, you can just keep the doctors in a TreeSet
.
Upvotes: 10