【问题标题】:Need to add Comparable without modifying the class that is comparing需要添加 Comparable 而不修改正在比较的类
【发布时间】:2019-11-21 20:40:35
【问题描述】:

代码:

LinkedBinarySearchTree <Pair<String, Integer>> at = new LinkedBinarySearchTree<>();
Pair<String, Integer> p = new Pair<>(str, dni);
at.insert(p);

Pair 是给我的一个类,它不是 java 类 Pair(如果 java 有一个默认的 pair 类,idk 但万一它有一个,这个不是那个)。

类对中没有定义compareTo,而insert 方法在某些时候使用了 compareTo,当它发生时它崩溃了。

我需要实现抽象类Comparable并从外部重写类中的compareTo方法,而不需要修改类Pair的代码,这意味着我必须从“外部”来做。

有没有办法做到这一点?

这是我之前做的:

public class MyComparator implements Comparator <Pair<String, Integer>> {
        @Override
        public int compare(Pair<String, Integer> o1, Pair<String, Integer> o2) {
           final Collator instance = Collator.getInstance();
            instance.setStrength(Collator.NO_DECOMPOSITION);


        if (!o1.getFirst().equals(o2.getFirst())){
            return o1.getFirst().compareTo(o2.getFirst());
        } else {
            return o1.getSecond().compareTo(o2.getSecond());
        }
    }
}

但它不适用于 Comparator,由于某种原因它必须是 Comparable 并且我不知道该怎么做,因为我无法参考(this):

public class MyComparable implements Comparable <Pair<String, Integer>> {
        @Override
        public int compareTo(Pair<String, Integer> o) {
           final Collator instance = Collator.getInstance();
            instance.setStrength(Collator.NO_DECOMPOSITION);

        //I can't use "this" here because ovbiously I'm not inside the class Pair so I don't know how to do it
        if (!this.getFirst().equals(o.getFirst())){   //I can't use "this"
            return this.getFirst().compareTo(o.getFirst());
        } else {
            return this.getSecond().compareTo(o.getSecond());
        }
    }
}

我需要帮助我一直在尝试自己寻找答案,但我没有想法......如果这个问题太简单或没有帮助,我很抱歉,但我在这里有点挣扎:/。

 

编辑: 我调试了程序,这就是它崩溃的地方,这就是为什么我 认为我需要 Comparable:

public class DefaultComparator<E> implements Comparator<E> {
    @Override
    public int compare(E a, E b) throws ClassCastException {
        return ((Comparable<E>) a).compareTo(b); //here
    }
}

【问题讨论】:

  • 您可能会感到困惑。仅仅因为你的比较器不工作并不意味着更好的比较器不是解决方案。
  • “崩溃”是什么意思?你有例外吗?如果有,是哪一个?是ClassCastException 吗?

标签: java comparator comparable


【解决方案1】:

你能不能用你自己的类扩展Pair并使用它?

public class MyPair<T, O> extends Pair<T, O> implements Comparable<MyPair<T, O>> {
    @Override
    public int compareTo(MyPair<T, O> other) {
           //logic to compare
    }
}

然后使用它

LinkedBinarySearchTree <MyPair<String, Integer>> at = new LinkedBinarySearchTree<>();

基于 cmets 编辑: 如果您知道Pair 中使用的对象类型本身就是Comparable,那么您可以使用有界泛型参数。所以上面的例子就变成了:

public class MyPair<T extends Comparable<T>, O extends Comparable<O>> extends Pair<T, O> implements Comparable<MyPair<T, O>> {
    @Override
    public int compareTo(MyPair<T, O> other) {
           //Now the compiler knows that T and O types are Comparable (that 
           //is they implement the Comparable interface) and 
           //this means their compareTo() can be used

           return this.getFirst().compareTo(other.getFirst()); 
    }
}

【讨论】:

  • 嘿,如果我做错了什么,但我不能使用 compareTo String 默认的 java 方法。例如这里:return this.getSecond().compareTo(other.getSecond()); 就像 this.getSecond() 返回一个对象而不是字符串
  • 您正在尝试通过调用 .compareTo()... 来比较这对中的 getSecond() 和另一对中的 getSecond()... 但 getSecond() 是 O 类型,可以是任何类型,因此编译器不知道这些是可比较的。您需要制作这些绑定的泛型参数...我将修改我原来的分析器...
  • @randomstudent2332 我已经扩展了我原来的答案......这有意义吗?
  • 是的!它现在可以工作并且不会崩溃,我非常感谢你,但是......是的,很抱歉一直打扰你,但还有一件事。如果我需要我的程序最后返回(在完成所有插入和东西之后) Pair 而不是 MyPair 有没有办法让它返回?就像一种将 MyPair 转换为 Pair 的方法。
  • 没关系。如果 Cat 扩展 Animal 则使用继承原则,然后 Cat 是 Cat 并且 Cat 也是动物...这意味着您可以将 Cat 转换为 Animal... 所以在您的情况下,您的意思是要返回 LinkedBinarySearchTree > 而不是 LinkedBinarySearchTree >?
【解决方案2】:

您可以创建一个包装器类来配对,而无需更改配对,但添加与包装器相当的功能,然后您需要将链表的泛型更改为 ComparablePair

class ComparablePair implements Comparable < ComparablePair > {
  private Pair < String,Integer > pair;
  @Override
  public int compareTo(ComparablePair o) {
    Pair otherPair = o.pair;
    //compare this.pair and otherpair here.
    return 0;
  }
}


LinkedBinarySearchTree <ComparablePair> at = new LinkedBinarySearchTree<>();

【讨论】:

  • 我不允许更改任何类的任何内容,因此我无法将我的链表泛型更改为 ComparablePair :/.
猜你喜欢
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 2018-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多