确保按元素中的字段排序的元素列表
Ensure List of elements ordered by field in element
使用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 lambda 有密切关系)没有为这种情况提供简单的解决方案,这让我很奇怪。
当然,我可以编写一些帮助方法并将其包装起来,但也许有人已经在库中完成了。有最简单的方法吗?类似于:
XLibrary.isOrdered(employees, Employee::getAge);
assertThat("Ordered by age",
Ordering.from(Comparator.comparing(Employee::getAge)).isOrdered(list);
或
assertThat("Ordered by age",
Ordering.natural().onResultOf(Employee::getAge).isOrdered(list);
使用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 lambda 有密切关系)没有为这种情况提供简单的解决方案,这让我很奇怪。 当然,我可以编写一些帮助方法并将其包装起来,但也许有人已经在库中完成了。有最简单的方法吗?类似于:
XLibrary.isOrdered(employees, Employee::getAge);
assertThat("Ordered by age",
Ordering.from(Comparator.comparing(Employee::getAge)).isOrdered(list);
或
assertThat("Ordered by age",
Ordering.natural().onResultOf(Employee::getAge).isOrdered(list);