真实开发中,类肯定很多,但是他们会执行相同的操作,如下
/**
* 获取单位统计销售数据
* @param query
*/
private void getAllTypeSalesUnitMap(OrdersSalesStatQuery query) {
List allTypeSalesUnitList = orderTimerService.listSlaveAllTypeOrdersStat(QueryType.UNIT, query);
allTypeSalesUnitPieceMap = allTypeSalesUnitList.stream().filter(item -> item.getUnitName().equals("件")).collect(Collectors.toMap(AllTypeOrdersSalesUnitStat::getOrderType,v->v,(v1,v2) -> v1));
allTypeSalesUnitPacketMap = allTypeSalesUnitList.stream().filter(item -> item.getUnitName().equals("包")).collect(Collectors.toMap(AllTypeOrdersSalesUnitStat::getOrderType,v->v,(v1,v2) -> v1));
}/**
* 获取dlt单位统计销售数据
* @param dltQuery
*/
private void getDltAllTypeSalesUnitMap(DltOrdersSalesStatQuery dltQuery) {
List dltAllTypeSalesUnitList = dltOrderCoreService.listSlaveAllTypeOrdersStat(QueryType.UNIT, dltQuery);
dltAllTypeSalesUnitPieceMap = dltAllTypeSalesUnitList.stream().filter(item -> item.getUnitName().equals("件")).collect(Collectors.toMap(DltAllTypeOrdersSalesUnitStat::getOrderType,v->v,(v1,v2) -> v1));
dltAllTypeSalesUnitPacketMap = dltAllTypeSalesUnitList.stream().filter(item -> item.getUnitName().equals("包")).collect(Collectors.toMap(DltAllTypeOrdersSalesUnitStat::getOrderType,v->v,(v1,v2) -> v1));
}/**
* 获取单位统计销售退货数据
* @param query
*/
private void getAllTypeRefundUnitMap(OrdersSalesStatQuery query) {
List allTypeRefundUnitList = orderTimerService.listSlaveAllTypeOrdersStat(QueryType.REFUND_UNIT, query);
allTypeRefundUnitPieceMap = allTypeRefundUnitList.stream().filter(item -> item.getUnitName().equals("件")).collect(Collectors.toMap(AllTypeOrdersRefundUnitStat::getOrderType,v->v,(v1,v2) -> v1));
allTypeRefundUnitPacketMap = allTypeRefundUnitList.stream().filter(item -> item.getUnitName().equals("包")).collect(Collectors.toMap(AllTypeOrdersRefundUnitStat::getOrderType,v->v,(v1,v2) -> v1));
}
然后Idea 就提示我 duplicates 了,所以要重构一下
要抽成方法的代码:
list.stream().filter(item -> item.getUnitName().equals("包")).collect(Collectors.toMap(DltAllTypeOrdersRefundUnitStat::getOrderType,v->v,(v1,v2) -> v1));
用java8函数式编程来进行方法的抽取:
private Map unitListConvertToMap(List list, Predicate supplier1, Function supplier) {
Map map = list.stream().filter(supplier1).collect(Collectors.toMap(supplier,v->v,(v1,v2) -> v1));
return map;
}
使用:
private void getDltAllTypeRefundUnitMap(DltOrdersSalesStatQuery dltQuery) {
List list = dltOrderCoreService.listSlaveAllTypeOrdersStat(QueryType.REFUND_UNIT, dltQuery);
dltAllTypeRefundUnitPieceMap = this.unitListConvertToMap(list,item -> item.getUnitName().equals("件"),DltAllTypeOrdersRefundUnitStat::getOrderType);
dltAllTypeRefundUnitPacketMap = this.unitListConvertToMap(list,item -> item.getUnitName().equals("包"),DltAllTypeOrdersRefundUnitStat::getOrderType);
}
【java|java8函数式编程--- 代码重构,抽出方法】
推荐阅读
- #|【SpringBoot】微服务中异步调用数据提交数据库的问题
- java|java 重构函数_包会,教你用Java函数式编程重构烂代码
- Spring|面试问题(Spring中Bean 的生命周期)
- Spring系列|Spring系列六(AOP概念和使用)
- Spring系列|Spring系列三(Spring Bean生命周期)
- Spring系列|Spring系列四(Spring Bean(深度讲解))
- Spring系列|Spring系列五(Spring怎么解决循环依赖)
- shell命令|Shell命令(curl之手动给中文参数编码)
- 3-SSM三大框架|Spring框架从入门到精通