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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top