[英]Java Generics Comparable | implement compareTo
I'm facing a problem with java generics. 我正面临java泛型的问题。 My plan is to implement a binary search tree (key & value) with generics with total ordering. 我的计划是使用具有总排序的泛型实现二叉搜索树(键和值)。 I started by creating this KeyValPair
and faced the problem of getting the right compareTo method. 我开始创建这个KeyValPair
并面临获得正确的compareTo方法的问题。
public class KeyValPair <V extends Comparable<V>>
implements Comparable<KeyValPair<V>>{
private int key;
private V value;
private KeyValPair<V> leftchild;
private KeyValPair<V> rightchild;
public KeyValPair(int k,V v){
key=k;
value=v;
}
public Comparable<?> getKey(){
return key;
}
public Comparable<?> getValue(){
return value;
}
public void setRightChild(KeyValPair<V> r){
rightchild=r;
}
public KeyValPair<V> getRightChild(KeyValPair<V> r){
return rightchild;
}
public void setLeftChild(KeyValPair<V> l){
leftchild=l;
}
public KeyValPair<V> getLeftChild(KeyValPair<V> l){
return leftchild;
}
@Override
public int compareTo(KeyValPair<V> toComp) {
if(this.getValue().compareTo(toComp.getValue())>0){
return -1;
}else if(this.getValue().compareTo(toComp.getValue())==0){
return 0;
}else{
return 1;
}
}
}
The if sentences in compareTo
are not acceppted and I think it's because of the fact that I overrode the compareTo
, but how should I compare generics? compareTo
中的if语句没有加入,我认为这是因为我重写了compareTo
,但我应该如何比较泛型呢?
Also tried Comparable
instead of K
with same result. 也试过Comparable
而不是K
同样的结果。
Best Regards 最好的祝福
EDIT: What compiler says: Multiple markers at this line - The method compareTo(capture#1-of ?) in the type Comparable is not applicable for the arguments (Comparable) - Line breakpoint:KeyValPair [line: 39] - compareTo(KeyValPair) 编辑:编译器说什么:此行的多个标记 - Comparable类型中的compareTo(捕获#1-of?)方法不适用于参数(可比较) - 行断点:KeyValPair [line:39] - compareTo(KeyValPair )
EDIT2: EDIT2:
UPDATED CODE: 更新的代码:
public class KeyValPair{
private int key;
private Comparable<?> value;
private KeyValPair leftchild;
private KeyValPair rightchild;
public KeyValPair(int k,Comparable<?> v){
key=k;
value=v;
}
public Comparable<?> getKey(){
return key;
}
public Comparable<?> getValue(){
return value;
}
public void setRightChild(KeyValPair r){
rightchild=r;
}
public KeyValPair getRightChild(KeyValPair r){
return rightchild;
}
public void setLeftChild(KeyValPair l){
leftchild=l;
}
public KeyValPair getLeftChild(KeyValPair l){
return leftchild;
}
}
Now i updated the code of the KEYVALPAIR, but if i test it with my BST Class with method adder as example: 现在我更新了KEYVALPAIR的代码,但如果我使用方法加法器的BST类测试它作为示例:
private void adder(KeyValPair current,KeyValPair toInsert) {
if(toInsert.getValue().compareTo(current.getValue())>0){
//dosomething
}
}
it throws: The method compareTo(capture#2-of ?) in the type Comparable is not applicable for the arguments (Comparable) 它抛出:Comparable类型中的compareTo(捕获#2-of?)方法不适用于参数(Comparable)
SOLUTION: 解:
I solved it by putting KEYVALPAIR as inner class to BST and use V extends Comparable. 我通过将KEYVALPAIR作为内部类添加到BST并使用V extends Comparable来解决它。 Works now, thanks for your help. 现在工作,谢谢你的帮助。
You don't need to cast key or value to comparable since V
is required to be comparable already. 您不需要将键或值转换为可比较的,因为V
必须已经可比较。 Doing so just makes it harder to use your class because now you have just a Comparable
instead of usable values or keys. 这样做只会让你更难使用你的类,因为现在你只有一个Comparable
而不是可用的值或键。
public int getKey() {
return key;
}
public V getValue() {
return value;
}
@Override
public int compareTo(KeyValPair<V> toComp) {
return -this.getValue().compareTo(toComp.getValue());
}
You should also consider to relax the requirement that V
must implement Comparable<V>
您还应该考虑放宽V
必须实现Comparable<V>
class KeyValPair<V extends Comparable<? super V>>
would allow eg classes like Apple extends Fruit implements Comparable<Fruit>
- those can still be ordered if they are comparable to super types. 允许像Apple extends Fruit implements Comparable<Fruit>
这样的类Apple extends Fruit implements Comparable<Fruit>
- 如果它们可以与超类型相媲美,那么仍然可以订购。
Your problem here is that you are required to compare V
to an other V
and you don't have the slightest idea about its runtime type. 您的问题是,您需要将V
与其他V
进行比较,并且您对其运行时类型没有任何想法。
It can be a String
or an Integer
and you obviously don't compare them the same way. 它可以是String
或Integer
,你显然不会以同样的方式比较它们。
So I think that your KeyValPair
should not implement Comparable
since its members ( V
instances) are already doing so: <V extends Comparable<V>>
. 所以我认为你的KeyValPair
不应该实现Comparable
因为它的成员( V
实例)已经这样做了: <V extends Comparable<V>>
。
If you want some comparison you can simply do something like: 如果你想进行一些比较,你可以简单地做一些事情:
leftChild.getValue().compareTo(rightChild.getValue());
getValue()
should also return Comparable<V>
, the wildcard is not necessary. getValue()
也应该返回Comparable<V>
,不需要通配符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.