Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 树映射不排序_Java_Treemap - Fatal编程技术网

Java 树映射不排序

Java 树映射不排序,java,treemap,Java,Treemap,这是我在博士班上的成绩 public int compareTo(Object o) { Doctor d = (Doctor)o; return (doctor_name.compareTo(d.doctor_name)); } 这是我的hashmap 这是我的树图 jTextArea.setText(""); int doctor_id = Integer.parseInt(jTextField2.getText()); String doctor_name = jText

这是我在博士班上的成绩

public int compareTo(Object o) {
    Doctor d = (Doctor)o;
    return (doctor_name.compareTo(d.doctor_name));
}
这是我的hashmap

这是我的树图

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);
它不会对医生的名字进行排序。为什么?

树映射根据元素的键而不是值对元素进行排序。 作为替代方案,您可以使用按医生姓名索引的树状图。 或者,根据您需要执行的操作,您可以将医生保留在树集中。

树映射根据元素的键而不是值对元素进行排序。 作为替代方案,您可以使用按医生姓名索引的树状图。
或者,根据你需要做什么,你可以把医生放在一个树集中。

我可以用doctorName设置的树来做,不是吗?如果你要使用一个集合,它将是一个排序好的医生集合,你可能还必须覆盖equals,这将允许像yourSet.containsd这样的测试。如果按名称使用地图,则可以使用map.getGregory House来获取Doctor对象。private TreeMap tmDoctor=new TreeMap;如果我这样做了,它现在可以工作了,但在博士类中Compariable的含义是什么?博士实现Compariable的事实将允许它出现在树集或树映射中,即用作树的键而不是值。这也意味着你可以在医生[]上使用Arrays.sort。我可以用doctorName设置的树来实现这一点,不是吗?如果你要使用一个集合,它将是一个医生的排序集合,你可能还必须覆盖equals,这将允许像yourSet.containsd这样的测试。如果按名称使用地图,则可以使用map.getGregory House来获取Doctor对象。private TreeMap tmDoctor=new TreeMap;如果我这样做了,它现在可以工作了,但在博士类中Compariable的含义是什么?博士实现Compariable的事实将允许它出现在树集或树映射中,即用作树的键而不是值。这也意味着您可以在医生[]上使用array.sort。
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());
}