如何使用Java8 lambda反向排序流?
我正在使用Java lambda对列表进行排序。
如何以相反的方式对其进行排序?
我看到了这篇文章,但是我想使用java 8 lambda。
这是我的代码(我用* -1)作为破解
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());
回答:
您可以调整在Java中如何以降序对ArrayList
.sorted((f1, f2) -> Long.compare(f2.lastModified(), f1.lastModified())
请注意, f2 是的第一个参数Long.compare,而不是第二个,因此结果将相反。
以上是 如何使用Java8 lambda反向排序流? 的全部内容, 来源链接: utcz.com/qa/418563.html

