[英]Java - TreeMap Object as Key or Value?
我需要一些对象的“容器”。 定义:
class DataSet implements Comparable {
public int id;
public String Date;
public double Value
public DataSetFSA (int id, String Date) {
this.id=id;
this.Date=Date;
}
@Override
public int compareTo(Object o) {
return 1;
}
}
对象具有一定顺序,不应被破坏! 如您所见,我的容器类本身已经有一个“ id”!
所以我的问题是现在我应该将对象作为键或值插入TreeMap吗?
DataSet ds1 = new DataSetFSA(1,"Val1");
DataSet ds2 = new DataSetFSA(2,"Val2");
...
TreeMap DataTreeMap = new TreeMap();
DataTreeMap.put(1,ds1); //as value
DataTreeMap.put(2,ds2);
//or as key (then the class has to implement comparable)
DataTreeMap.put(ds1,1); //as key but then the value doesnt has a function anymore
DataTreeMap.put(ds2,2);
如果您只是在寻找一个已排序的(唯一的)容器,则没有理由使用TreeMap
- TreeSet
似乎非常适合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.