• 请不要在回答技术问题时复制粘贴 AI 生成的内容
wleexi
V2EX  ›  程序员

下面 Java 代码怎么用 Lambda 表示

  •  
  •   wleexi · Dec 13, 2018 · 4312 views
    This topic created in 2884 days ago, the information mentioned may be changed or developed.
    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 replies  •  2018-12-14 11:04:27 +08:00
    chanchan
    chanchan
    Dec 13, 2018
    1
    Optional.ofNullable(obj).ifPresent(item -> {

    });
    wleexi
    wleexi
    OP
    Dec 13, 2018
    2
    @chanchan 每个属性都要写一个?
    chanchan
    chanchan
    Dec 13, 2018
    3
    这种还是 StringUtils.isNoneEmpty(,,,,)吧
    xwbz2018
    xwbz2018
    Dec 13, 2018
    4
    ```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
    wleexi
    OP
    Dec 13, 2018
    5
    @chanchan 类型是 Long。。。
    我就是来问问有什么更好的办法做检查并设置值
    wleexi
    wleexi
    OP
    Dec 13, 2018
    6
    @xwbz2018 那查询完了之后设置值呢。。按照 index ?
    xwbz2018
    xwbz2018
    Dec 13, 2018
    7
    @wleexi 魔法一下可以的(这块可以写到 shopView 类里),总不能上反射吧
    wleexi
    wleexi
    OP
    Dec 13, 2018
    8
    @xwbz2018 恩 我疑惑的地方就在这边。最后都是调用一样的方法 想精简
    chanchan
    chanchan
    Dec 13, 2018
    9
    @wleexi 兄嘚,你就不能开动一下你的脑筋吗
    wleexi
    wleexi
    OP
    Dec 13, 2018
    10
    @chanchan 表达式这块实在不熟 正在学习中 谢谢提点
    xwbz2018
    xwbz2018
    Dec 13, 2018
    11
    @wleexi 这 getter,setter 是没法省了。。。regionService.getById 如果支持多参数的话,还可以优化一下
    wleexi
    wleexi
    OP
    Dec 13, 2018
    12
    @xwbz2018 这个倒是可以
    youngxhui
    youngxhui
    Dec 13, 2018 via Android
    13
    这么多空判断不如换成 kotlin
    corningsun
    corningsun
    Dec 13, 2018
    14
    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
    lhx2008
    Dec 13, 2018 via Android
    15
    楼上的写法应该是楼主想要的了,不过说实话不如原来的
    johnniang
    johnniang
    Dec 13, 2018
    16
    可以考虑改造 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
    lhx2008
    Dec 13, 2018 via Android
    17
    想了下,还可以定义一个静态方法
    static void <T> setProperty(Callable<Long> idGetter, Callable<T> regionGetter, Runnable setter)
    里面是 14 楼的代码
    然后传的时候用 lambda
    具体怎么玩楼主研究一下,手机不想打了
    lhx2008
    lhx2008
    Dec 13, 2018 via Android
    18
    不一定是 17 楼的代码,原来的代码也行,反正是抽象出来了,可读性也还行吧,泛型不一定要
    lhx2008
    lhx2008
    Dec 13, 2018 via Android
    19
    @lhx2008 17->14
    lhx2008
    lhx2008
    Dec 13, 2018 via Android
    20
    static <T,R> void setProperty(Callable<Long> idGetter, Callable<T> regionGetter, Consumer<R> setter)
    重新谢了一遍
    xwbz2018
    xwbz2018
    Dec 13, 2018 via Android
    21
    @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
    Kaiv2
    Dec 13, 2018 via Android
    22
    这不太符合 Lambda 的使用场景。。。
    Nickwongfree
    Nickwongfree
    Dec 13, 2018❤️1
    23
    可以简洁一点,分支太多,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
    Nickwongfree
    Dec 13, 2018
    24
    @Nickwongfree
    line5 应为 setFunction.apply()
    Charkey
    Charkey
    Dec 13, 2018
    25
    函数式接口似乎不错,楼上有了
    binbinyouliiii
    binbinyouliiii
    Dec 13, 2018
    26
    有时候强上 Lambda 不觉得可读性会变差吗
    wleexi
    wleexi
    OP
    Dec 14, 2018
    27
    @binbinyouliiii 你说的对。这个题目的目的是学习
    wleexi
    wleexi
    OP
    Dec 14, 2018
    28
    @Nickwongfree 好像不行,setCityDisp 是 void
    008px
    008px
    Dec 14, 2018 via Android
    29
    首先你要明确 Lambda 表达式的使用条件,不是所有都能用的
    CasualYours
    CasualYours
    Dec 14, 2018
    30
    @wleexi Function 是接收一个参数,返回一个结果。Consumer 接收一个参数,无返回结果。

    改成这样就行了

    ```
    void setLocation(String locationId, Consumer<String> setFunction) {
    //...
    }
    ```
    CasualYours
    CasualYours
    Dec 14, 2018
    31
    @CasualYours 方法内部还要改一句 setFunction.accept(region.getRegionName());
    Raymon111111
    Raymon111111
    Dec 14, 2018
    32
    不是 lambda 的场景 别硬套了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4277 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 68ms · UTC 05:38 · PVG 13:38 · LAX 21:38 · JFK 00:38
    ♥ Do have faith in what you're doing.