使用Guava可以确保以这种方式升序:
import com.google.common.collect.Ordering;
import io.predictor.dao.ohlcv.OhlcvHm;
import static java.util.stream.Collectors.toList;
assertThat("Ordered by age", Ordering.natural().isOrdered(
employees.stream().map(Employee::getAge).collect(toList())));
对我来说,Guava(因为它与Java lambdas的所有密切关系)并不能为这种情况提供简单的解决方案。 当然,我可以写一些帮助方法并包装它,但也许有人已经在库中完成了它。有一种最简单的方法吗?类似的东西:
XLibrary.isOrdered(employees, Employee::getAge);
答案 0 :(得分:5)
assertThat("Ordered by age",
Ordering.from(Comparator.comparing(Employee::getAge)).isOrdered(list);
或
assertThat("Ordered by age",
Ordering.natural().onResultOf(Employee::getAge).isOrdered(list);