假设有这么个对象
@Data
@Accessors(chain = true)
public class OrderMapVo {
private String launchDate;
private String sexCode;
private String categoryCode;
}
要实现基于launchDate有序分组,同时分组内的数据按sexCode,categoryCode排序,空值放最后。可以借助Collectors.collectingAndThen实现
List<OrderMapSeriesVo> list = new ArrayList<>();
// 填充数据...
// 排序
TreeMap<String, List<OrderMapVo>> dayMap = list.stream()
.collect(Collectors.groupingBy(OrderMapVo::getLaunchDate, TreeMap::new,
Collectors.collectingAndThen(
Collectors.toList(),
sub->sub.stream().sorted(
Comparator.comparing(OrderMapVo::getSexCode, Comparator.nullsLast(Comparator.naturalOrder()))
.thenComparing(OrderMapVo::getCategoryCode, Comparator.nullsLast(Comparator.naturalOrder()))
).collect(Collectors.toList())
)));
网友评论