java泛型排序,如何对Java中的泛型类型列表进行排序

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 and Bar extends BaseEntity implements Comparable, and sorting of lists of Foos or Bars 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 Foos or Bars. This code, for example, fails to compile:

public class UtilityClass {

...bunch of stuff...

List values;

public List sort() {

Collections.sort(values);

return values;

}

...more methods...

}

with the error message Bound mismatch: The generic method sort(List) of type Collections is not applicable for the arguments (List). The inferred type T is not a valid substitute for the bounded parameter >

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 to BaseEntity, the compiler tells me that I've got problems now because my Foo class is trying to implement both Comparable and Comparable, which evidently is not allowed.

I know I could sidestep this issue by dropping the implements Comparable and just implementing Comparable, 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> {...}

That specifies that T must be both a BaseEntity and Comparable to itself.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值