Formatting dates inside a Function<T,R>

Viewed 52

I am trying to format a date inside a Functional Interface but I don't know if it is possible

SimpleDateFormat dt1 = new SimpleDateFormat("ddmmyyyyy");

List<MenuPrice> menuPrices = findAll(restaurant);

menuPrices.parallelStream()
          .collect(Collectors.groupingBy(dt1.format(MenuPrice::getUpdateDate)));
2 Answers

It's possible, but not with a method reference:

Map<String,List<MenuPrice>>
    menuPrices.parallelStream()
              .collect(Collectors.groupingBy(m -> dt1.format(m.getUpdateDate())));

You could create a method for that btw, to make things slightly more readable:

private static String formatUpdatedDate(MenuPrice menu){ 
     return dt1.format(menu.getUpdatedDate());
}

And use it:

 .collect(Collectors.groupingBy(YourClass::formatUpdatedDate)
Related