TreeMap 不排序
public int compareTo(Object o) {
Doctor d = (Doctor)o;
return (doctor_name.compareTo(d.doctor_name));
}
这是我在医生类中的比较
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);
这是我的哈希图
,这是我的树图
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());
}
它不会对医生姓名进行排序。为什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TreeMap
根据元素的键而不是值对元素进行排序。作为替代方案,您可以使用按医生姓名索引的
TreeMap
。或者,根据您需要执行的操作,您可以将医生保留在
TreeSet
中。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
.