有一段代码,感觉可以优化,不知道怎么把公共的地方抽取出来,求大神指导

f1696014041 2020-08-13 10:40:35
List<WarnConfig> conflist = warnConfigService.getByIccid();

Map<String , List<String>> listMap = new HashMap<>();
Map<String , String> imeiMap = new HashMap<>();

conflist.forEach(conf ->{
String owner = conf.getUsername(); //得到用户名
List<ForecastDevice> forecastList = forecastDeviceService.getByOwner(owner , null);
forecastList.forEach(fst -> {
imeiList.add(fst.getImei());
imeiMap.put(fst.getImei(),owner);
});

List<WeatherDevice> weaList = weatherDeviceService.getByOwner(owner, null);
weaList.forEach(wea ->{
imeiList.add(wea.getImei());
imeiMap.put(wea.getImei(),owner);
});

List<MoistureDevice> mosList = moistureDeviceService.getByOwner(owner, null);
mosList.forEach(mos ->{
imeiList.add(mos.getImei());
imeiMap.put(mos.getImei(),owner);
});

List<LampLastItemVo> lamplist = lampServiceClient.getByOwner(owner);//用户下的杀虫灯设备
lamplist.forEach(lamp ->{
imeiList.add(lamp.getSn());
imeiMap.put(lamp.getSn(),owner);
});
});

...全文
1280 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复 1
引用 6 楼 f1696014041 的回复:
[quote=引用 5 楼 &Low_Key 的回复:]不是每段重复太多的代码都要抽出来,代码短不代表好; 要性能,易读性,可拓展性,,四个原则都要好。。。。盲目的codereveiw是错误的选择
盲目的codereveiw肯定是不推荐的, 就像设计模式一样,不能为了模式而模式, 但是有一点, 你可以不进行codereveiw , 但不能不知道怎么codereveiw , 简单的说就是: 可以,但没必要 , 首先要可以才行. 我现在采用的是三楼的方法,三楼回复的方式,不只这一个地方可以用,以后遇到其他问题的时候,在三楼这种方式上进行变化(ps: java8 stream用的还不是很熟练,有些地方还是要多学习学习) [/quote] 建议你测测性能,用lamda优化后的上面那段代码,不出意外,性能降低了
qybao 2020-08-13
  • 打赏
  • 举报
回复
抽成一个方法就好了
    public static void appendMap(List<?> list, String owner, List<String> imeiList , Map<String, String> imeiMap) {
for (Object o : list) {
String k = null;
if (o instanceof XXX) { //根据你自己的类型修改XXX,YYY
k = ((XXX)o).getImei();
} else if (o instanceof YYY) {
k = ((YYY)o).getSn();
}
if (k != null) {
imeiList.add(k);
imeiMap.put(k, owner);
}
}
}
// 调用
List<String> imeiList = new ArrayList<>();
Map<String , String> imeiMap = new HashMap<>();
conflist.forEach(conf ->{
String owner = conf.getUsername(); //得到用户名
List<ForecastDevice> forecastList = forecastDeviceService.getByOwner(owner , null);
appendMap(forecastList, owner, imeiList, imeiMap);

List<WeatherDevice> weaList = weatherDeviceService.getByOwner(owner, null);
appendMap(weaList, owner, imeiList, imeiMap);

....
});
大隐藏于寺 2020-08-13
  • 打赏
  • 举报
回复
你可以测试下面的代码,看能运行不,我写完了没有测试.


public class OperatorDemo {

    public static void main(String[] args) {
        List<WarnConfig> conflist = new ArrayList<>();

        Map<String, List<String>> listMap = new HashMap<>();
        Map<String, String> imeiMap = new HashMap<>();
        ArrayList<String> imeiList = new ArrayList<>();

        conflist.forEach(conf -> {
            String owner = conf.getUsername(); //得到用户名
            List<ForecastDevice> forecastList = new ArrayList<>();
            List<WeatherDevice> weaList = new ArrayList<>();
            List<MoistureDevice> mosList = new ArrayList<>();
            List<LampLastItemVo> lamplist = new ArrayList<>();//用户下的杀虫灯设备
            operation(forecastList, imeiList, imeiMap, ForecastDevice::getImei, owner);
            operation(weaList, imeiList, imeiMap, WeatherDevice::getImei, owner);
            operation(mosList, imeiList, imeiMap, MoistureDevice::getImei, owner);
            operation(lamplist, imeiList, imeiMap, LampLastItemVo::getSn, owner);
        });
    }
    public static <T> void operation(List<T> operationList, List<String> resultList, Map<String, String> resultMap, Function<T, String> mapper, String owner) {
        List<String> list = operationList.stream().map(mapper).collect(Collectors.toList());
        resultList.addAll(list);
        Map<String, String> map = list.stream().collect(Collectors.toMap(Function.identity(), s -> owner));
        resultMap.putAll(map);
    }


}
dkwuxiang 2020-08-13
  • 打赏
  • 举报
回复
***Device 实现统一 接口   ParentDevice
ParentDevice 中 : 统一接口: doSomething  :每个 实现重现,里面放你需要的代码
public void add(List<ParentDevice> list){
     list.forEach(********);
}


这样行嘛
f1696014041 2020-08-13
  • 打赏
  • 举报
回复
f1696014041 2020-08-13
  • 打赏
  • 举报
回复
引用 5 楼 &Low_Key 的回复:
不是每段重复太多的代码都要抽出来,代码短不代表好; 要性能,易读性,可拓展性,,四个原则都要好。。。。盲目的codereveiw是错误的选择
盲目的codereveiw肯定是不推荐的, 就像设计模式一样,不能为了模式而模式, 但是有一点, 你可以不进行codereveiw , 但不能不知道怎么codereveiw , 简单的说就是: 可以,但没必要 , 首先要可以才行. 我现在采用的是三楼的方法,三楼回复的方式,不只这一个地方可以用,以后遇到其他问题的时候,在三楼这种方式上进行变化(ps: java8 stream用的还不是很熟练,有些地方还是要多学习学习)
  • 打赏
  • 举报
回复
不是每段重复太多的代码都要抽出来,代码短不代表好; 要性能,易读性,可拓展性,,四个原则都要好。。。。盲目的codereveiw是错误的选择

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧