java8Stream多条件分组统计
camelliaSettleVO类
@Data
@ToString
public class SettleVO implements Serializable {
private Long id;
/**
* ⽇期
*/
private String ndDate;
/
**
* 始发中⼼所在的城市
*/
private String startCityId;
/**
* 始发城市名称
*/
private String startCityName;
/**
* ⽬的城市
*/
private String endCityId;
累了的英文/**
* ⽬的城市名称
*/
private String endCityName;
/**
* 耗时
*/
private BigDecimal timeConsuming;
/**
* 机票价格
*/
private Double ticketPrice;
/**
* 公⾥数
*/
private Integer distance;
}
SettleGroupVO类
@Data
@ToString
public class SettleGroupVO implements Serializable {
/
**
fiance*耗时
*/
private BigDecimal timeConsuming;
/**
* 机票价格
*/
private Double ticketPrice;surveyed
/**
* 公⾥数
*/
在线金山词霸
private Integer distance;
}
主函数:
public class StaticService {
dull
public static void main(String[] args) {
//制造测试数据
List<SettleVO> ttleVOList = new ArrayList<>();
for (int i = 10; i < 30; i++) {
final SettleVO ttleVO = new SettleVO();
二级建造师报名表ttleVO.tSendDate("2020-07-" + String.valueOf(i));
ttleVO.tStartCityId("100" + (i % 5));
ttleVO.tEndCityId("200" + (i % 5));
ttleVO.tTicketPrice((double) (i % 5));
ttleVO.tTimeConsuming(new BigDecimal(i % 5));
ttleVO.tDistance(100 * (i % 5));
ttleVOList.add(ttleVO);
}
//按照维度分组统计
final Map<SettleVO, SettleGroupVO> groupVOMap = ttleVOList.stream()
.upingBy(n -> getGroupKey(n), List(), m -> { final Double ticketPrice = m.stream().mapToDouble(SettleVO::getTicketPrice).sum();
final BigDecimal timeConsuming = m.stream().map(e -> e.getTimeConsuming()).reduce(BigDecimal::
add).get();
final Integer distance = m.stream().mapToInt(SettleVO::getDistance).sum();
SettleGroupVO ttleGroupVO = new SettleGroupVO();
ttleGroupVO.tTicketPrice(ticketPrice);
ttleGroupVO.tTimeConsuming(timeConsuming);
ttleGroupVO.tDistance(distance);
return ttleGroupVO;
})));
System.out.println("groupByMap=" + groupVOMap.size());
groupVOMap.forEach((k, v) -> {
System.out.String());
System.out.String());
});
ggh
}
//获取分组维度
private static SettleVO getGroupKey(final SettleVO vo) { final SettleVO groupKey = new SettleVO();magista
groupKey.SendDate());plates
groupKey.StartCityId());
groupKey.EndCityId());
return groupKey;
}
}
测试结果:
View Code