
if (null != shopView.getProvinceId()) { Region region = regionService.getById(shopView.getProvinceId()); shopView.setProvinceDisp(region.getRegionName()); } if (null != shopView.getCityId()) { Region region = regionService.getById(shopView.getCityId()); shopView.setCityId(region.getRegionName(); } if (null != shopView.getDistrictId()) { Region region = regionService.getById(shopView.getDistrictId()); shopView.setDistrictDisp(region.getRegionName()); } if (null != shopView.getStreetId()) { Region region = regionService.getById(shopView.getStreetId()); shopView.setStreetDisp(region.getRegionName()); } 1 chanchan 2018-12-13 14:57:21 +08:00 Optional.ofNullable(obj).ifPresent(item -> { }); |
3 chanchan 2018-12-13 17:27:23 +08:00 这种还是 StringUtils.isNoneEmpty(,,,,)吧 |
4 xwbz2018 2018-12-13 17:28:58 +08:00 ```java String[] regiOnNames= Stream.of(shopView.getProvinceId(), shopView.getCityId(), shopView.getDistrictId(), shopView.getStreetId()) .filter(Objects::nonNull) .map(regionService::getById) .map(Region::getRegionName) .toArray(String[]::new); ``` 这样可行不,设置值到 shopView 那部分比较魔法 |
11 xwbz2018 2018-12-13 18:06:25 +08:00 @wleexi 这 getter,setter 是没法省了。。。regionService.getById 如果支持多参数的话,还可以优化一下 |
13 youngxhui 2018-12-13 18:14:17 +08:00 via Android 这么多空判断不如换成 kotlin |
14 corningsun 2018-12-13 18:15:57 +08:00 Optional.ofNullable(showView.getProvinceId()).map(RegionService::getById).map(Region::getRegionName).ifPresent(showView::setProvinceDisp); Optional.ofNullable(showView.getCityId()).map(RegionService::getById).map(Region::getRegionName).ifPresent(showView::setCityDisp); Optional.ofNullable(showView.getDistrictId()).map(RegionService::getById).map(Region::getRegionName).ifPresent(showView::setDistrictDisp); Optional.ofNullable(showView.getStreetId()).map(RegionService::getById).map(Region::getRegionName).ifPresent(showView::setStreetDisp); |
15 lhx2008 2018-12-13 18:19:42 +08:00 via Android 楼上的写法应该是楼主想要的了,不过说实话不如原来的 |
16 johnniang 2018-12-13 18:19:43 +08:00 可以考虑改造 getById()方法 ```java Optional<Entity> getById(Integer id) { if(id == null || id <= 0) { return Optional.empty(); } ... } ``` 结合一楼 ```java regionService.getById(shopView.getProvinceId()).ifPresent(region -> shopView.setProvinceDisp(region.getRegionName())); ... ``` |
17 lhx2008 2018-12-13 18:24:52 +08:00 via Android 想了下,还可以定义一个静态方法 static void <T> setProperty(Callable<Long> idGetter, Callable<T> regionGetter, Runnable setter) 里面是 14 楼的代码 然后传的时候用 lambda 具体怎么玩楼主研究一下,手机不想打了 |
18 lhx2008 2018-12-13 18:26:13 +08:00 via Android 不一定是 17 楼的代码,原来的代码也行,反正是抽象出来了,可读性也还行吧,泛型不一定要 |
20 lhx2008 2018-12-13 18:36:31 +08:00 via Android static <T,R> void setProperty(Callable<Long> idGetter, Callable<T> regionGetter, Consumer<R> setter) 重新谢了一遍 |
21 xwbz2018 2018-12-13 18:43:58 +08:00 via Android @xwbz2018 抱歉,随手一写写出 bug 了。应该是: String[] regiOnNames= Stream.of(shopView.getProvinceId(), shopView.getCityId(), shopView.getDistrictId(), shopView.getStreetId()) .map(id -> id == null ? null : regionService.getById(id)) .map(Region::getRegionName) .toArray(String[]::new); |
22 Kaiv2 2018-12-13 19:53:37 +08:00 via Android 这不太符合 Lambda 的使用场景。。。 |
23 Nickwongfree 2018-12-13 21:24:46 +08:00 可以简洁一点,分支太多,4 条基本可以抽象出来新函数 void setLocation(String locationId, Function<String, Boolean> setFunction) { if (locationId!=null) { Region region = regionService.getById(locationId); setFunction(region.getRegionName()); } } 4 条调用如下 setLocation(shopView.getProvinceId(), showView::setProvinceDisp) setLocation(shopView. getCityId(), showView:: setCityDisp) .... |
24 Nickwongfree 2018-12-13 21:34:38 +08:00 @Nickwongfree line5 应为 setFunction.apply() |
25 Charkey 2018-12-13 21:36:43 +08:00 函数式接口似乎不错,楼上有了 |
26 binbinyouliiii 2018-12-13 23:34:47 +08:00 有时候强上 Lambda 不觉得可读性会变差吗 |
27 wleexi OP @binbinyouiiii 你说的对。这个题目的目的是学习 |
28 wleexi OP @Nickwongfree 好像不行,setCityDisp 是 void |
29 008px 2018-12-14 10:00:22 +08:00 via Android 首先你要明确 Lambda 表达式的使用条件,不是所有都能用的 |
30 CasualYours 2018-12-14 10:05:40 +08:00 @wleexi Function 是接收一个参数,返回一个结果。Consumer 接收一个参数,无返回结果。 改成这样就行了 ``` void setLocation(String locationId, Consumer<String> setFunction) { //... } ``` |
31 CasualYours 2018-12-14 10:08:27 +08:00 @CasualYours 方法内部还要改一句 setFunction.accept(region.getRegionName()); |
32 Raymon111111 2018-12-14 11:04:27 +08:00 不是 lambda 的场景 别硬套了 |