0

I want to use List.distinctBy to filter lists provided by (Javaslang) I added this dependency in my pom.xml

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

but when I use it in the code

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

I have a compilation error:

The method distinctBy(MenuPriceByDay::getUpdateLocalDate) is undefined for the type 
 Stream<MenuPriceByDay>
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301

1 Answers1

0

Well, List.distinctBy is a method on io.vavr.collection.List, and you are trying to call it on a java.util.stream.Stream.

You can use e.g. StreamEx instead:

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

But for this specific case you really don't need it because you are doing a map by the same function afterwards. So it should be equivalent to

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

(assuming getUpdateLocalDate is a pure function).

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487