比较两个泛型在compareTo()中。

huangapple go评论77阅读模式
英文:

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&lt;T extends Comparable&lt;T&gt;&gt; implements SortedSet&lt;T&gt;

and another class outside of SkipListSet class:

class Node&lt;T extends Comparable&lt;T&gt;&gt;

The second one acts as a wrapper class that contains the following:

		T data;
		List&lt;Node&lt;T&gt;&gt; tower;
		Node&lt;T&gt; nextNode = null;
		Node&lt;T&gt; prevNode = null;

When I try implementing compareTo() method in my Node class:

		public int compareTo(T somePayLoad) {
			if (this.data &lt; somePayLoad)
				return -1;
			else if (this.data &gt; somePayLoad)
				return 1;
			else
				return 0;
		}

I get the following error:

SkipListSet.java:171: error: bad operand types for binary operator &#39;&lt;&#39;
                        if (this.data &lt; somePayLoad)
                                      ^
  first type:  T
  second type: T
  where T is a type-variable:
    T extends Comparable&lt;T&gt; 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&lt;NodeData&gt; list = new TestSkipListSet&lt;NodeData&gt;();
    TestSkipListSet&lt;NodeData2&gt; list2 = new TestSkipListSet&lt;NodeData2&gt;();
    class NodeData implements Comparable&lt;NodeData&gt; {
        int value;
        @Override
        public int compareTo(@NonNull NodeData o) {
            return this.value - o.value;
        }
    }
    class NodeData2 implements Comparable&lt;NodeData2&gt; {
        TestUser value;
        @Override
        public int compareTo(@NonNull NodeData2 o) {
            return this.value.age-o.value.age;
        }
    }
    class TestUser{
        int age;
    }
    public class TestSkipListSet&lt;T extends Comparable&lt;T&gt;&gt; implements SortedSet&lt;T&gt;{
       ...
    }

huangapple
  • 本文由 发表于 2020年7月23日 11:28:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63046414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定