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

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

Comparing two generics in compareTo()

问题

我有以下的类签名:

  1. public class SkipListSet<T extends Comparable<T>> implements SortedSet<T>

还有一个在SkipListSet类之外的类:

  1. class Node<T extends Comparable<T>>

第二个类充当一个包装类,包含以下内容:

  1. T data;
  2. List<Node<T>> tower;
  3. Node<T> nextNode = null;
  4. Node<T> prevNode = null;

当我尝试在我的Node类中实现compareTo()方法时:

  1. public int compareTo(T somePayLoad) {
  2. if (this.data < somePayLoad)
  3. return -1;
  4. else if (this.data > somePayLoad)
  5. return 1;
  6. else
  7. return 0;
  8. }

我得到以下错误:

  1. SkipListSet.java:171: error: bad operand types for binary operator '<'
  2. if (this.data < somePayLoad)
  3. ^
  4. first type: T
  5. second type: T
  6. where T is a type-variable:
  7. T extends Comparable<T> declared in class SkipListSet.Node

为什么我不能在compareTo方法中比较两个T类型的数据?

英文:

I have the following class signatures:

  1. public class SkipListSet&lt;T extends Comparable&lt;T&gt;&gt; implements SortedSet&lt;T&gt;

and another class outside of SkipListSet class:

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

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

  1. T data;
  2. List&lt;Node&lt;T&gt;&gt; tower;
  3. Node&lt;T&gt; nextNode = null;
  4. Node&lt;T&gt; prevNode = null;

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

  1. public int compareTo(T somePayLoad) {
  2. if (this.data &lt; somePayLoad)
  3. return -1;
  4. else if (this.data &gt; somePayLoad)
  5. return 1;
  6. else
  7. return 0;
  8. }

I get the following error:

  1. SkipListSet.java:171: error: bad operand types for binary operator &#39;&lt;&#39;
  2. if (this.data &lt; somePayLoad)
  3. ^
  4. first type: T
  5. second type: T
  6. where T is a type-variable:
  7. 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

你不能在对象上使用 '<' 或 '>'。我认为你需要的是:

  1. public int compareTo(T somePayLoad) {
  2. return this.data.compareTo(somePayLoad.data);
  3. }

(添加空值检查)。

英文:

You can't use '<' or '>' on objects. I think what you need is:

  1. public int compareTo(T somePayLoad) {
  2. return this.data.compareTo(somePayLoad.data);
  3. }

(Add null checks).

答案2

得分: 0

不需要编写compareTo方法。
T类应该实现Comparable接口。

  1. TestSkipListSet<NodeData> list = new TestSkipListSet<NodeData>();
  2. TestSkipListSet<NodeData2> list2 = new TestSkipListSet<NodeData2>();
  1. class NodeData implements Comparable<NodeData> {
  2. int value;
  3. @Override
  4. public int compareTo(@NonNull NodeData o) {
  5. return this.value - o.value;
  6. }
  7. }
  1. class NodeData2 implements Comparable<NodeData2> {
  2. TestUser value;
  3. @Override
  4. public int compareTo(@NonNull NodeData2 o) {
  5. return this.value.age - o.value.age;
  6. }
  7. class TestUser {
  8. int age;
  9. }
  10. }
  1. public class TestSkipListSet<T extends Comparable<T>> implements SortedSet<T> {
  2. ...
  3. }
英文:

You don't need to write the compareTo method.
T class should implement Comparable inteface.

  1. TestSkipListSet&lt;NodeData&gt; list = new TestSkipListSet&lt;NodeData&gt;();
  2. TestSkipListSet&lt;NodeData2&gt; list2 = new TestSkipListSet&lt;NodeData2&gt;();
  1. class NodeData implements Comparable&lt;NodeData&gt; {
  2. int value;
  3. @Override
  4. public int compareTo(@NonNull NodeData o) {
  5. return this.value - o.value;
  6. }
  7. }
  1. class NodeData2 implements Comparable&lt;NodeData2&gt; {
  2. TestUser value;
  3. @Override
  4. public int compareTo(@NonNull NodeData2 o) {
  5. return this.value.age-o.value.age;
  6. }
  7. }
  8. class TestUser{
  9. int age;
  10. }
  1. public class TestSkipListSet&lt;T extends Comparable&lt;T&gt;&gt; implements SortedSet&lt;T&gt;{
  2. ...
  3. }

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:

确定