代码之家  ›  专栏  ›  技术社区  ›  Nuñito Calzada

使用Vavr List.distinctBy

  •  0
  • Nuñito Calzada  · 技术社区  · 6 年前

    我想使用List.distinctBy来过滤(Javaslang)提供的列表 我在pom.xml中添加了这个依赖项

       <dependency>
    <groupId>io.vavr</groupId>
    <artifactId>vavr-kotlin</artifactId>
    <version>0.10.0</version>
    

    但当我在代码中使用它时

    menuPriceByDayService
                    .findAllOrderByUpdateDate(menu, DateUtils.semestralDate(), 26)
                    .stream()
                    .distinctBy(MenuPriceByDay::getUpdateLocalDate)
                    .map(cp -> cp.getUpdateLocalDate())
                    .sorted()
                    .forEach(System.out::println);
    

    The method distinctBy(MenuPriceByDay::getUpdateLocalDate) is undefined for the type 
     Stream<MenuPriceByDay>
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Alexey Romanov    6 年前

    好, List.distinctBy 是上的方法 io.vavr.collection.List ,而你正试图在 java.util.stream.Stream .

    你可以用例如。 StreamEx 取而代之的是:

    StreamEx.of(
        menuPriceByDayService
            .findAllOrderByUpdateDate(menu, DateUtils.semestralDate(), 26)
            .stream())
        .distinct(MenuPriceByDay::getUpdateLocalDate)
        .map // etc
    

    但对于这种特殊情况,你真的不需要它,因为你正在做一个 map 同样的功能 之后。所以它应该等同于

    menuPriceByDayService
        .findAllOrderByUpdateDate(menu, DateUtils.semestralDate(), 26)
        .stream()
        .map(cp -> cp.getUpdateLocalDate())
        .distinct()
        .sorted()
        .forEach(System.out::println);
    

    (假设 getUpdateLocalDate