Map<Long, String> failMap = Maps.newHashMap(); for (Long wmPoiId :…
- User
Map<Long, String> failMap = Maps.newHashMap(); for (Long wmPoiId : getPoiListFromUrl(producerVO.getPubAreaVO().getPoiIdUrl())) { try { if (isGary(wmPoiId)) { if(isOpen()) { SendMsgToFriendsResponse resp = corpWechatDomainService.sendMsgToFriends(wmPoiId, genMsg(basicVO, wmPoiId)); if (Objects.nonNull(resp) && resp.getCode() != 0) { failMap.put(wmPoiId, resp.getMsg()); } } else { log.info("WmADMarketingWechatServiceImpl.wechatPush info:{}", genMsg(basicVO, wmPoiId)); } } } catch (Exception e) { failMap.put(wmPoiId, e.getMessage()); } } return failMap;
代码可以优化吗?
- Assistant
以下是对代码的一些优化建议:
-
避免每次循环都调用getPoiListFromUrl()方法,可以在循环外先获取poiList,然后再使用。这样可以减少不必要的网络访问和计算。
-
使用Lambda表达式可以让代码更加简洁,例如:
poiList.stream().filter(this::isGary).forEach(wmPoiId -> { try { if(isOpen()) { SendMsgToFriendsResponse resp = corpWechatDomainService.sendMsgToFriends(wmPoiId, genMsg(basicVO, wmPoiId)); if (Objects.nonNull(resp) && resp.getCode() != 0) { failMap.put(wmPoiId, resp.getMsg()); } } else { log.info("WmADMarketingWechatServiceImpl.wechatPush info:{}", genMsg(basicVO, wmPoiId)); } } catch (Exception e) { failMap.put(wmPoiId, e.getMessage()); } });
-
使用方法引用可以进一步简化代码,例如:
poiList.stream().filter(this::isGary).forEach(wmPoiId -> { try { if(isOpen()) { SendMsgToFriendsResponse resp = corpWechatDomainService.sendMsgToFriends(wmPoiId, genMsg(basicVO, wmPoiId)); if (Objects.nonNull(resp) && resp.getCode() != 0) { failMap.put(wmPoiId, resp.getMsg()); } } else { log.info("WmADMarketingWechatServiceImpl.wechatPush info:{}", genMsg(basicVO, wmPoiId)); } } catch (Exception e) { failMap.put(wmPoiId, e.getMessage()); } });
-
- User
最终代码
- Assistant
以下是基于上述优化建议进行修改后的代码:
List<Long> poiList = getPoiListFromUrl(producerVO.getPubAreaVO().getPoiIdUrl()); Map<Long, String> failMap = new ConcurrentHashMap<>(); poiList.stream() .filter(this::isGary) .forEach(wmPoiId -> { try { if(isOpen()) { SendMsgToFriendsResponse resp = corpWechatDomainService.sendMsgToFriends(wmPoiId, genMsg(basicVO, wmPoiId)); if (resp != null && resp.getCode() != 0) { failMap.put(wmPoiId, resp.getMsg()); } } else { log.info("WmADMarketingWechatServiceImpl.wechatPush info:{}", genMsg(basicVO, wmPoiId)); } } catch (Exception e) { failMap.put(wmPoiId, e.getMessage()); } }); return failMap;主要的修改包括:
-
在循环外先获取poiList,避免每次循环都调用getPoiListFromUrl()方法。
-
使用Lambda表达式和方法引用简化代码。
-
使用ConcurrentHashMap代替HashMap,以支持多线程并发操作。
-
在判断resp是否为null时,使用resp != null代替Objects.nonNull(resp),因为resp的类型为SendMsgToFriendsResponse,如果非空则不为null,不必再判断非空性。
-
- User
为什么会 每次循环都调用getPoiListFromUrl()方法? 什么时候用 resp != null,什么时候用Objects.nonNull(resp)
- Assistant
在原始代码的循环中,调用了getPoiListFromUrl(producerVO.getPubAreaVO().getPoiIdUrl())方法,每次循环都会执行该方法获取poiList。这样做会增加网络请求以及计算的开销,因此建议将其移至循环外,只调用一次获取整个poiList。
当需要判断一个对象是否为null时,可以使用resp != null或者Objects.nonNull(resp)。这两种写法的效果是相同的,都可以判断对象是否为空,不过建议使用resp != null的方式来判断,因为这种方式更简洁直观,也因为Objects非null方法要从ObjectUtils这个工具类中寻找。
- User
在原始代码的循环中,调用了getPoiListFromUrl(producerVO.getPubAreaVO().getPoiIdUrl())方法,每次循环都会执行该方法获取poiList。这样做会增加网络请求以及计算的开销,因此建议将其移至循环外,只调用一次获取整个poiList。 这个结论有相关依据吗?
- Assistant
我的建议是基于常识和经验做出的,而非具体分析代码的运行状况。每次循环都调用getPoiListFromUrl()方法虽然不会造成致命影响,但会多次发送网络请求和计算操作。尤其对于poiList较大的情况下,这样做的性能影响会更大。因此,将其放到循环外,只调用一次获取整个poiList,可以优化程序性能,减轻系统负担。这种优化方式也是编程中常见的优化手段之一。