如何在Java中实现通用的`max(Comparable a,Comparable b)`函数? [英] How to implement a generic `max(Comparable a, Comparable b)` function in Java?

查看:243
本文介绍了如何在Java中实现通用的`max(Comparable a,Comparable b)`函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个通用的max函数,它需要两个 Comparable s。



到目前为止,我具有

  public static< T extends Comparable<?>> T max(T a,T b){
if(a == null){
if(b == null)return a;
else return b;
}
if(b == null)
return a;
返回a.compareTo(b)> 0? a:b;
}

无法与

 类型Comparable< capture#5-of?>中的方法compareTo(capture#5-of?)>不适用于参数(T)

我认为这是说<在 Comparable<?> 中的c $ c>?可以被解释为参数a的一种类型,而参数b则被解释为另一种类型,不能比较。



我如何从这个洞里挖掘出自己? / div>

为获得最佳结果,您应该使用 public static< T extends Comparable< ;? super T>> T max(T a,T b)



< T的问题扩展了Comparable< > 是说这个类型T可以和某种类型相比,但是你不知道那个类型是什么。当然,常识意味着一个实现了Comparable的类应该至少能够与其本身相媲美(即能够与它自己类型的对象进行比较),但从技术上讲,没有什么能够阻止类A实现 Comparable< B> ,其中A和B没有任何关系。 < T extends Comparable< T>> 解决了这个问题。 但是有一个细微的问题。假设类X实现 Comparable< X> ,并且我有一个扩展X的类Y.所以,类Y自动实现 Comparable< X> 通过继承。 Y类不能实现 Comparable< Y> ,因为类不能使用不同类型的参数实现两次接口。这并不是一个问题,因为Y的实例是X的实例,所以Y可以与Y的所有实例相比较。但问题是,您不能使用类型Y与< T extends Comparable< ; T>> T max(T a,T b)函数,因为Y没有实现 Comparable< Y> 。边界太严格了。 < T扩展了Comparable< super T>>< / code>修复了这个问题,因为T足以与T的某个超类型(其中包含所有T实例)相媲美。回顾规则PECS - 生产者扩展,消费者 super - 在这种情况下,可比较是一个消费者(它需要一个对象进行比较),所以 super 是有道理的。



这是Java库中所有排序和排序函数使用的类型边界。


I'm trying to write a generic max function that takes two Comparables.

So far I have

public static <T extends Comparable<?>> T max(T a, T b) {
    if (a == null) {
        if (b == null) return a;
        else return b;
    }
    if (b == null)
        return a;
    return a.compareTo(b) > 0 ? a : b;
}

This fails to compiles with

The method compareTo(capture#5-of ?) in the type Comparable<capture#5-of ?> is not applicable for the arguments (T)

What I think this is saying is that that the ? in Comparable<?> may be interpreted as one type for parameter a, and another for parameter b, so that they can't be compared.

How do I dig myself out of this hole?

解决方案

For best results you should use public static <T extends Comparable<? super T>> T max(T a, T b).

The problem with <T extends Comparable<?>> is that this says that the type T is comparable to some type, but you don't know what that type is. Of course, common sense would dictate that a class which implements Comparable should be able to be comparable to at least itself (i.e. be able to compare to objects of its own type), but there is technically nothing preventing class A from implementing Comparable<B>, where A and B have nothing to do with each other. <T extends Comparable<T>> solves this problem.

But there's a subtle problem with that. Suppose class X implements Comparable<X>, and I have a class Y that extends X. So class Y automatically implements Comparable<X> by inheritance. Class Y can't also implement Comparable<Y> because a class cannot implement an interface twice with different type parameters. This is not really a problem, since instances of Y are instances of X, so Y is comparable to all instances of Y. But the problem is that you cannot use the type Y with your <T extends Comparable<T>> T max(T a, T b) function, because Y doesn't implement Comparable<Y>. The bounds are too strict. <T extends Comparable<? super T>> fixes the problem, because it is sufficient for T to be comparable to some supertype of T (which would include all T instances). Recall the rule PECS - producer extends, consumer super - in this case, Comparable is a consumer (it takes in an object to compare against), so super makes sense.

This is the type bounds used by all of the sorting and ordering functions in the Java library.

这篇关于如何在Java中实现通用的`max(Comparable a,Comparable b)`函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆