使用的框架:Spring
使用的ORM: hibernate
我有两个类(class)
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
是一个接口(interface),所以无法确定是否可排序。您必须使用正确的实现,例如 TreeSet .如果你想
强调它是一个排序集,你应该使用 SortedSet
接口(interface)。 TreeSet
实现了 SortedSet
。
或者,您可以使用 List
,然后使用 Collections.sort
对其进行排序。
关于java - 在 Java 中对集合进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53426073/