如何对Java中的泛型类型列表进行排序 [英] How to sort a list of generic types in Java
问题描述
我有一组所有共享一些共同属性的类,所以我使它们都扩展了一个公共基类, BaseEntity
。所以我有,例如<code> Foo扩展BaseEntity 和 Bar扩展BaseEntity
。
我也希望这些 Foo
和 Bar
对象的列表可排序,所以我实现了可比
。我将类定义为 Foo extends BaseEntity implements Comparable< Foo>
和 Bar扩展BaseEntity implements Comparable< Bar>
,并且对 Foo
s或 Bar
的列表进行排序按预期工作 - 当然还有排序的细节在不同的亚类中是不同的。但是当我不知道我是否会有 Foo
s或 Bar
秒。例如,此代码无法编译:
public class UtilityClass< T extends BaseEntity> {
...一堆东西...
列表< T>值;
public List< T> sort(){
Collections.sort(values);
返回值;
}
...更多方法...
}
与错误消息绑定不匹配:类型集合的泛型方法sort(List< T>)不适用于参数(List< T>)。推断的类型T不是有界参数的有效替代< T extends Comparable< ;?我认为问题在于我试图对
BaseEntity 列表进行排序, code>对象,而
BaseEntity
本身并没有实现 Comparable
。但是现在我面临一个问题:唯一明智的做法是使 BaseEntity
对象与其他 BaseEntity
对象相比,但是当我在 BaseEntity
中添加了 implements Comparable< BaseEntity>
,编译器告诉我现在有问题了,因为我的 Foo
类试图实现 Comparable< BaseEntity>
和 Comparable< Foo>
,这显然是不允许的。
我知道我可以通过删除 implements Comparable< Foo> $来回避这个问题c $ c>然后执行
Comparable< BaseEntity>
,但是我的 compareTo
方法必须做丑陋的投射,我认为这正是使用泛型应该避免的问题。
我真正想要做的是在签名中指定, BaseEntity
它的所有子类都是 Comparable
,但只限于相同的实例子类。
感激地收到了任何帮助。
使用交点类型,如下所示:
public class MyList< T extends BaseEntity&可比< T>> {...}
指定T必须是 BaseEntity
和 可比较
自身。
I have a set of classes that all share some common attributes, so I made them all extend a common base class, BaseEntity
. So I have, for example Foo extends BaseEntity
and Bar extends BaseEntity
.
I also want lists of these Foo
and Bar
objects to be sortable, so I have implemented Comparable
. I have the classes defined as Foo extends BaseEntity implements Comparable<Foo>
and Bar extends BaseEntity implements Comparable<Bar>
, and sorting of lists of Foo
s or Bar
s works as expected - and, of course, the details of the sorting are different in the different subclasses. But I can't work out how to make my sorting work when I don't know in advance whether I'll have Foo
s or Bar
s. This code, for example, fails to compile:
public class UtilityClass<T extends BaseEntity> {
...bunch of stuff...
List<T> values;
public List<T> sort() {
Collections.sort(values);
return values;
}
...more methods...
}
with the error message Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (List<T>). The inferred type T is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>
I think the problem is that I am attempting to sort a list of BaseEntity
objects, and BaseEntity
itself doesn't implement Comparable
. But now I face a problem: the only sensible thing to make BaseEntity
objects comparable to is other BaseEntity
objects, but when I add implements Comparable<BaseEntity>
to BaseEntity
, the compiler tells me that I've got problems now because my Foo
class is trying to implement both Comparable<BaseEntity>
and Comparable<Foo>
, which evidently is not allowed.
I know I could sidestep this issue by dropping the implements Comparable<Foo>
and just implementing Comparable<BaseEntity>
, but then my compareTo
methods will have to do ugly casting, and I thought that was exactly the sort of problem using generics was supposed to avoid.
What I really want to do is specify in the signature of BaseEntity
that all its subclasses will be Comparable
, but only to instances of the same subclass.
Any assistance gratefully received. Thanks!
Use an intersection type, like this:
public class MyList<T extends BaseEntity & Comparable<T>> {...}
That specifies that T must be both a BaseEntity
and Comparable
to itself.
这篇关于如何对Java中的泛型类型列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!