如何在 Java 中对泛型类型列表进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14577910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort a list of generic types in Java
提问by Dave Mulligan
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
.
我有一组类都共享一些共同的属性,所以我让它们都扩展了一个共同的基类BaseEntity
. 所以我有,例如Foo extends BaseEntity
和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:
我还希望这些Foo
和Bar
对象的列表是可排序的,所以我实现了Comparable
. 我将类定义为Foo extends BaseEntity implements Comparable<Foo>
and Bar extends BaseEntity implements Comparable<Bar>
,并且Foo
s 或Bar
s列表的排序按预期工作 - 当然,排序的细节在不同的子类中是不同的。但是当我事先不知道我是否会有Foo
s 或Bar
s时,我无法弄清楚如何使我的排序工作。例如,此代码无法编译:
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>>
带有错误消息 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.
我认为问题在于我试图对BaseEntity
对象列表进行排序,而BaseEntity
它本身并没有实现Comparable
. 但现在我面临一个问题:使BaseEntity
对象与其他对象相比较的唯一明智的做法是BaseEntity
,当我添加implements Comparable<BaseEntity>
到 时BaseEntity
,编译器告诉我我现在遇到了问题,因为我的Foo
类试图同时实现Comparable<BaseEntity>
和Comparable<Foo>
,这显然是不允许。
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.
我知道我可以通过删除implements Comparable<Foo>
并只实现来回避这个问题Comparable<BaseEntity>
,但是我的compareTo
方法将不得不进行丑陋的转换,我认为这正是使用泛型应该避免的那种问题。
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.
我真正想做的是在签名中指定BaseEntity
其所有子类都将是Comparable
,但仅限于同一子类的实例。
Any assistance gratefully received. Thanks!
感谢任何帮助。谢谢!
采纳答案by Dolda2000
Use an intersection type, like this:
使用交集类型,如下所示:
public class MyList<T extends BaseEntity & Comparable<T>> {...}
That specifies that T must be both a BaseEntity
andComparable
to itself.
这指定 T 必须既是 aBaseEntity
又Comparable
是其自身。
回答by notXX
Don't use Collections.sort(List<T>)
, use Collections.sort(Lst<T>, Comparator<? extends T>)
instead. Write the comparation code in the comparator.
不要使用Collections.sort(List<T>)
,Collections.sort(Lst<T>, Comparator<? extends T>)
而是使用。在比较器中编写比较代码。
回答by Anju Maaka
You could make a super-simple sort method that can handle pretty much any List type
你可以制作一个超级简单的排序方法,它几乎可以处理任何 List 类型
public class Tools {
public static <E> void sortList(List<E> list, Comparator<E> comparator) {
Collections.sort(list, comparator);
}
// Any other utility methods/resources you want in here
}
This means you can call this method to sort any List so long as the given List and Comparator are of the same Type as each other. It would work even without the base class you have (BaseEntity)
这意味着只要给定的 List 和 Comparator 的类型相同,您就可以调用此方法对任何 List 进行排序。即使没有您拥有的基类,它也能工作(BaseEntity)
回答by Pascut
Try this:
试试这个:
static <T extends Comparable<? super T>> sort(T[] array);
This is the most general specification to accomplish the task. Basically, it asserts, that T is a type which can be compared to itself.
这是完成任务的最通用的规范。基本上,它断言 T 是一种可以与其自身进行比较的类型。