下面 Java 代码怎么用 Lambda 表示

2018-12-13 14:51:07 +08:00
 wleexi
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());
        }
3312 次点击
所在节点    程序员
32 条回复
chanchan
2018-12-13 14:57:21 +08:00
Optional.ofNullable(obj).ifPresent(item -> {

});
wleexi
2018-12-13 17:19:47 +08:00
@chanchan 每个属性都要写一个?
chanchan
2018-12-13 17:27:23 +08:00
这种还是 StringUtils.isNoneEmpty(,,,,)吧
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 那部分比较魔法
wleexi
2018-12-13 17:45:03 +08:00
@chanchan 类型是 Long。。。
我就是来问问有什么更好的办法做检查并设置值
wleexi
2018-12-13 17:46:20 +08:00
@xwbz2018 那查询完了之后设置值呢。。按照 index ?
xwbz2018
2018-12-13 17:50:27 +08:00
@wleexi 魔法一下可以的(这块可以写到 shopView 类里),总不能上反射吧
wleexi
2018-12-13 17:51:37 +08:00
@xwbz2018 恩 我疑惑的地方就在这边。最后都是调用一样的方法 想精简
chanchan
2018-12-13 17:52:01 +08:00
@wleexi 兄嘚,你就不能开动一下你的脑筋吗
wleexi
2018-12-13 17:58:21 +08:00
@chanchan 表达式这块实在不熟 正在学习中 谢谢提点
xwbz2018
2018-12-13 18:06:25 +08:00
@wleexi 这 getter,setter 是没法省了。。。regionService.getById 如果支持多参数的话,还可以优化一下
wleexi
2018-12-13 18:07:57 +08:00
@xwbz2018 这个倒是可以
youngxhui
2018-12-13 18:14:17 +08:00
这么多空判断不如换成 kotlin
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);
lhx2008
2018-12-13 18:19:42 +08:00
楼上的写法应该是楼主想要的了,不过说实话不如原来的
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()));

...
```
lhx2008
2018-12-13 18:24:52 +08:00
想了下,还可以定义一个静态方法
static void <T> setProperty(Callable<Long> idGetter, Callable<T> regionGetter, Runnable setter)
里面是 14 楼的代码
然后传的时候用 lambda
具体怎么玩楼主研究一下,手机不想打了
lhx2008
2018-12-13 18:26:13 +08:00
不一定是 17 楼的代码,原来的代码也行,反正是抽象出来了,可读性也还行吧,泛型不一定要
lhx2008
2018-12-13 18:26:52 +08:00
@lhx2008 17->14
lhx2008
2018-12-13 18:36:31 +08:00
static <T,R> void setProperty(Callable<Long> idGetter, Callable<T> regionGetter, Consumer<R> setter)
重新谢了一遍

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/517227

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX