【问题标题】:Using Vavr List.distinctBy使用 Vavr List.distinctBy
【发布时间】:2019-08-07 20:48:58
【问题描述】:

我想使用 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>

【问题讨论】:

    标签: java collections functional-programming java-stream vavr


    【解决方案1】:

    好吧,List.distinctByio.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是一个纯函数)。

    【讨论】:

      猜你喜欢
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      相关资源
      最近更新 更多