Question

This line compiles

List<Trade> trades = otrades.stream()
                     .sorted(Comparator.comparing(t -> t.getMeta().getTradeDate()))
                     .collect(Collectors.toList()));

But adding a 'thenComparing' does not

List<Trade>trades = otrades.stream()
                   .sorted(Comparator.comparing(t -> t.getMeta().getTradeDate())
                   .thenComparing(t -> t.getName()))
                   .collect(Collectors.toList()));

Compiler error is that it can not resolve getMeta().

(As there don't appear to be any errors in the code I'm assuming the problem is in IntelliJ).

Thanks

Was it helpful?

Solution

For a reason I don't understand, type inference fails in the second case. But you can give the type of t

List<Trade>trades = otrades.stream()//*******
   .sorted(Comparator.comparing(     (Trade t) -> t.getMeta().getTradeDate())
   .thenComparing(t -> t.getName()))
   .collect(Collectors.toList()));

In your example, the compiler finds that t is something else that a Trade (probably Object). That's why the method getMeta() cannot be found.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top