Java 可比<;T>;不为treemap工作
我正在尝试构建一个树映射,并且已经为密钥类实现了我自己的可比较接口,但是我仍然得到一个“不能转换为可比较”的异常,我真的不明白为什么。以下是我的代码的相关部分: 接口:Java 可比<;T>;不为treemap工作,java,treemap,comparable,Java,Treemap,Comparable,我正在尝试构建一个树映射,并且已经为密钥类实现了我自己的可比较接口,但是我仍然得到一个“不能转换为可比较”的异常,我真的不明白为什么。以下是我的代码的相关部分: 接口: public interface Comparable<T> { public int compareTo(T o); } 然后返回PriorityBuy无法解析为Compariable的异常。但我似乎已经建立了这个程序,这样应该可以工作了。那么……谁能帮我看看为什么它不是?你不能创建自己的可比的界面;您必须使
public interface Comparable<T> {
public int compareTo(T o);
}
然后返回PriorityBuy无法解析为Compariable的异常。但我似乎已经建立了这个程序,这样应该可以工作了。那么……谁能帮我看看为什么它不是?你不能创建自己的
可比的界面;您必须使用内置的java.lang.Compariable
您不能创建自己的Compariable
界面;您必须使用内置的java.lang.Comparable
好的,那么我如何使用java.lang.Comparable实现一个可比较的接口呢?这是否等于删除了我编写的接口?编辑:没关系,我是个白痴。谢谢你帮我看。:)好的,那么如何使用java.lang.comparable实现一个可比较的接口呢?这是否等于删除了我编写的接口?编辑:没关系,我是个白痴。谢谢你帮我看。:)
public class PriorityBuy implements Comparable<PriorityBuy>{
protected double _priceDouble;
protected DollarValue _priceDollarValue = new DollarValue(_priceDouble);
protected Price _price = new Price(_priceDollarValue);
protected long _time;
public PriorityBuy(Price price, long time) {
this._price = price;
this._time = time;
}
public Price getPrice() {
return _price;
}
public long getTime() {
return _time;
}
/**
* The following provides a new choice of hash function for this class. The reason is that
* we need to create a new equals method to match the compareTo method, and we need to know
* that equal objects return equal hashCode.
*/
@Override
public int hashCode() {
return (int) (((_price.hashCode())*13)^(_time));
}
/**
* We re-implement the equals method to match our compareTo method, which depends on both price
* and on time.
*/
@Override
public boolean equals(Object o) {
if(! (o instanceof PriorityBuy)) {
return false;
}
PriorityBuy p = (PriorityBuy) o;
if(p.getPrice().getDollarValue().getValue() == _price.getDollarValue().getValue() && p.getTime() == _time) {
return true;
}
return false;
}
/**
* We are writing a compareTo method so that this class can implement Comparable<Priority>, which
* in turn allows any treemap constructed with Priority as the class of keys to order the tree
* according to the ordering defined by the compareTo method defined below, instead of using
* the "natural ordering" as it usually does.
*/
@Override
public int compareTo(PriorityBuy a) {
if(a.getPrice().getDollarValue().getValue() > this.getPrice().getDollarValue().getValue()) {
return -1;
}
else if(a.getPrice().getDollarValue().getValue() < this.getPrice().getDollarValue().getValue()) {
return 1;
}
else if(a.getTime() < this.getTime()) {
return -1;
}
else if(a.getTime() > this.getTime()) {
return 1;
}
else {
return 0;
}
}
}
public void addToBuyBook(OrderBookMessage obm) throws Exception {
Order order = this.createOrder(obm);
TreeMap<PriorityBuy,Order> buyBookTree = _buyBook.get(obm.getTicker());
buyBookTree.put(order.getPriorityBuy(), order);
_buyBook.put(obm.getTicker(), buyBookTree);
this.addBuyOrder(obm.getOrderID(), obm.getTicker(), obm.getLimitPrice(), obm.getQuantity());
}
public void testAddToBuyBook() throws Exception {
OrderBookManager manager = new OrderBookManager();
OrderBookMessage obm1 = new OrderBookMessage("IBM");
OrderBookMessage obm2 = new OrderBookMessage("IBM");
manager.addToBuyBook(obm1);
manager.addToBuyBook(obm2);
}