我正在使用Java Lambda对列表进行排序。
如何以相反的方式进行排序?
我看到了这篇帖子,但我想使用Java 8 Lambda。
这是我的代码(我使用*-1作为hack):
Arrays.asList(files).stream()
.filter(file -> isNameLikeBaseLine(file, baseLineFile.getName()))
.sorted(new Comparator<File>() {
public int compare(File o1, File o2) {
int answer;
if (o1.lastModified() == o2.lastModified()) {
answer = 0;
} else if (o1.lastModified() > o2.lastModified()) {
answer = 1;
} else {
answer = -1;
}
return -1 * answer;
}
})
.skip(numOfNewestToLeave)
.forEach(item -> item.delete());
forEachOrdered
иҖҢдёҚжҳҜforEach
гҖӮ - HolgerforEach
是在skip
之后执行的,也就是说,在我的筛选过滤之后,我并不关心顺序。 - Elad Benda2sort
→skip
→ (unordered)forEach
如何工作的理解是正确的,并且在今天的 JREs 中确实是这样实现的,但是在2015年之前,当前面的评论被提出时,这确实是一个问题(你可以在 这个问题 中了解更多)。 - Holger