// 返回结果为Stream, price为 Bigdecimal 类型, 已经实现Comparable(自然排序)接口的compareTo()方法
buyDepthData.stream().sorted(Comparator.comparing(DepthData::getPrice));
buyDepthData.stream().sorted(Comparator.comparing(DepthData::getPrice).reversed());
// 对当前list排序
buyDepthData.sort(Comparator.comparing(DepthData::getPrice)
// 倒序
.reversed());
还可以参考这篇文章
https://dzone.com/articles/java-8-comparator-how-to-sort-a-list
// 加和
buyDepthData.stream()
.map(DepthData::getPrice)
.reduce(BigDecimal.ZERO, BigDecimal::add)
List<BigDecimal> decimals = Lists.newArrayList(BigDecimal.ZERO, BigDecimal.TEN, BigDecimal.ONE);
System.out.println(decimals);
decimals.sort(Comparator.reverseOrder());
System.out.println(decimals);
decimals.sort(BigDecimal::compareTo);
System.out.println(decimals);
输出
[0, 10, 1]
[10, 1, 0]
[0, 1, 10]
网友评论