質問

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