V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wleexi
V2EX  ›  程序员

下面 Java 代码怎么用 Lambda 表示

  •  
  •   wleexi · 2018-12-13 14:51:07 +08:00 · 3303 次点击
    这是一个创建于 1954 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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());
            }
    
    32 条回复    2018-12-14 11:04:27 +08:00
    chanchan
        1
    chanchan  
       2018-12-13 14:57:21 +08:00
    Optional.ofNullable(obj).ifPresent(item -> {

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

    ...
    ```
    lhx2008
        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
    具体怎么玩楼主研究一下,手机不想打了
    lhx2008
        18
    lhx2008  
       2018-12-13 18:26:13 +08:00 via Android
    不一定是 17 楼的代码,原来的代码也行,反正是抽象出来了,可读性也还行吧,泛型不一定要
    lhx2008
        19
    lhx2008  
       2018-12-13 18:26:52 +08:00 via Android
    @lhx2008 17->14
    lhx2008
        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)
    重新谢了一遍
    xwbz2018
        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);
    Kaiv2
        22
    Kaiv2  
       2018-12-13 19:53:37 +08:00 via Android
    这不太符合 Lambda 的使用场景。。。
    Nickwongfree
        23
    Nickwongfree  
       2018-12-13 21:24:46 +08:00   ❤️ 1
    可以简洁一点,分支太多,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)
    ....
    Nickwongfree
        24
    Nickwongfree  
       2018-12-13 21:34:38 +08:00
    @Nickwongfree
    line5 应为 setFunction.apply()
    Charkey
        25
    Charkey  
       2018-12-13 21:36:43 +08:00
    函数式接口似乎不错,楼上有了
    binbinyouliiii
        26
    binbinyouliiii  
       2018-12-13 23:34:47 +08:00
    有时候强上 Lambda 不觉得可读性会变差吗
    wleexi
        27
    wleexi  
    OP
       2018-12-14 08:52:55 +08:00
    @binbinyouliiii 你说的对。这个题目的目的是学习
    wleexi
        28
    wleexi  
    OP
       2018-12-14 09:03:59 +08:00
    @Nickwongfree 好像不行,setCityDisp 是 void
    008px
        29
    008px  
       2018-12-14 10:00:22 +08:00 via Android
    首先你要明确 Lambda 表达式的使用条件,不是所有都能用的
    CasualYours
        30
    CasualYours  
       2018-12-14 10:05:40 +08:00
    @wleexi Function 是接收一个参数,返回一个结果。Consumer 接收一个参数,无返回结果。

    改成这样就行了

    ```
    void setLocation(String locationId, Consumer<String> setFunction) {
    //...
    }
    ```
    CasualYours
        31
    CasualYours  
       2018-12-14 10:08:27 +08:00
    @CasualYours 方法内部还要改一句 setFunction.accept(region.getRegionName());
    Raymon111111
        32
    Raymon111111  
       2018-12-14 11:04:27 +08:00
    不是 lambda 的场景 别硬套了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   916 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:50 · PVG 04:50 · LAX 13:50 · JFK 16:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.