2 回答

TA貢獻2041條經驗 獲得超4個贊
該findAllByMatch方法正在返回一個List<MatchEvent>.
并且該List.sort(someComparator)方法返回void,即它不返回任何內容,因為它就地對列表進行排序。所以你不能將調用鏈接到forEach(someConsumer).
解決您的問題的一種方法是使用 aStream而不是 a List:
List<MatchEventMobileApp> matchEventMobileApp = matchEventService
.findAllByMatch(SOME_MATCH)
.stream()
.sorted(Comparator.comparing(MatchEvent::getDateReceived))
.map(de -> new MatchEventMobileApp(de)) // or MatchEventMobileApp::new
.collect(Collectors.toList()); // better collect to a new list instead of
// adding to an existing one within forEach
這樣一來,你現在有工作Stream,其sorted方法返回另一個Stream(已排序的),可以在其上調用終端操作,即collect,forEach,anyMatch等等。
另一種可能性是將列表提取到變量中并使用它:
List<MatchEvent> list = matchEventService.findAllByMatch(SOME_MATCH);
list.sort(Comparator.comparing(MatchEvent::getDateReceived));
list.forEach(de -> matchEventMobileApp.add(new MatchEventMobileApp(de)));

TA貢獻1865條經驗 獲得超7個贊
List<MatchEventMobileApp> matchEventMobileApp = matchEventService .findAllByMatch(“JVT”) .stream() .sorted(Comparator.comparing(MatchEvent::getDateReceived)) .map(MatchEventMobileApp::new) .collect(Collectors.toList());
添加回答
舉報