英文:
Comparing two generics in compareTo()
问题
我有以下的类签名:
public class SkipListSet<T extends Comparable<T>> implements SortedSet<T>
还有一个在SkipListSet类之外的类:
class Node<T extends Comparable<T>>
第二个类充当一个包装类,包含以下内容:
		T data;
		List<Node<T>> tower;
		Node<T> nextNode = null;
		Node<T> prevNode = null;
当我尝试在我的Node类中实现compareTo()方法时:
		public int compareTo(T somePayLoad) {
			if (this.data < somePayLoad)
				return -1;
			else if (this.data > somePayLoad)
				return 1;
			else
				return 0;
		}
我得到以下错误:
SkipListSet.java:171: error: bad operand types for binary operator '<'
                        if (this.data < somePayLoad)
                                      ^
  first type:  T
  second type: T
  where T is a type-variable:
    T extends Comparable<T> declared in class SkipListSet.Node
为什么我不能在compareTo方法中比较两个T类型的数据?
英文:
I have the following class signatures:
public class SkipListSet<T extends Comparable<T>> implements SortedSet<T>
and another class outside of SkipListSet class:
class Node<T extends Comparable<T>>
The second one acts as a wrapper class that contains the following:
		T data;
		List<Node<T>> tower;
		Node<T> nextNode = null;
		Node<T> prevNode = null;
When I try implementing compareTo() method in my Node class:
		public int compareTo(T somePayLoad) {
			if (this.data < somePayLoad)
				return -1;
			else if (this.data > somePayLoad)
				return 1;
			else
				return 0;
		}
I get the following error:
SkipListSet.java:171: error: bad operand types for binary operator '<'
                        if (this.data < somePayLoad)
                                      ^
  first type:  T
  second type: T
  where T is a type-variable:
    T extends Comparable<T> declared in class SkipListSet.Node
Why is it that I can't compare two types of T data in my compareTo method?
答案1
得分: 2
你不能在对象上使用 '<' 或 '>'。我认为你需要的是:
public int compareTo(T somePayLoad) {
    return this.data.compareTo(somePayLoad.data);
}
(添加空值检查)。
英文:
You can't use '<' or '>' on objects. I think what you need is:
public int compareTo(T somePayLoad) {
    return this.data.compareTo(somePayLoad.data);
}
(Add null checks).
答案2
得分: 0
不需要编写compareTo方法。
T类应该实现Comparable接口。
TestSkipListSet<NodeData> list = new TestSkipListSet<NodeData>();
TestSkipListSet<NodeData2> list2 = new TestSkipListSet<NodeData2>();
class NodeData implements Comparable<NodeData> {
    int value;
    @Override
    public int compareTo(@NonNull NodeData o) {
        return this.value - o.value;
    }
}
class NodeData2 implements Comparable<NodeData2> {
    TestUser value;
    @Override
    public int compareTo(@NonNull NodeData2 o) {
        return this.value.age - o.value.age;
    }
    class TestUser {
        int age;
    }
}
public class TestSkipListSet<T extends Comparable<T>> implements SortedSet<T> {
   ...
}
英文:
You don't need to write the compareTo method.
T class should implement Comparable inteface.
    TestSkipListSet<NodeData> list = new TestSkipListSet<NodeData>();
    TestSkipListSet<NodeData2> list2 = new TestSkipListSet<NodeData2>();
    class NodeData implements Comparable<NodeData> {
        int value;
        @Override
        public int compareTo(@NonNull NodeData o) {
            return this.value - o.value;
        }
    }
    class NodeData2 implements Comparable<NodeData2> {
        TestUser value;
        @Override
        public int compareTo(@NonNull NodeData2 o) {
            return this.value.age-o.value.age;
        }
    }
    class TestUser{
        int age;
    }
    public class TestSkipListSet<T extends Comparable<T>> implements SortedSet<T>{
       ...
    }
专注分享java语言的经验与见解,让所有开发者获益!

评论