【发布时间】:2019-04-24 20:23:00
【问题描述】:
使用的框架:Spring
使用的 ORM:休眠
我有两门课
class BatchExceptionDetails{
...
private Set<BatchExceptionComments> batchExceptionComments;
}
class BatchExceptionComments implements Comparable<BatchExceptionComments>{
...
@Override
public int compareTo(BatchExceptionComments o) {
// TODO Auto-generated method stub
return this.getAddedOn().compareTo(o.getAddedOn());
}
}
它们使用一对多映射进行映射。
BatchExceptionDetails 中有一组 BatchExceptionComments。
我想根据日期对集合进行排序。 BatchExcpetionComment 具有 java.util.Date 类型的属性,即 addedOn。我希望最新的评论成为 set 的第一个元素。
我收到的集合没有排序。请您指导我哪里出错了。
提前致谢
【问题讨论】:
-
您使用的是
SortedSet吗?还是其他类型的Set? -
Collections.sort(batchExceptionComments); -
@Lorelorelore 这仅适用于列表,不适用于集合。
-
集合未排序。请改用列表。
-
您不能只使用
Set,因为Set是一个接口。你在使用Set的什么实现?
标签: java spring hibernate sorting collections