我想用 Lambda 对列表进行排序:
List<Message> messagesByDeviceType = new ArrayList<Message>();
messagesByDeviceType.sort((Message o1, Message o2)->o1.getTime()-o2.getTime());
但是我得到了这个编译错误:
Multiple markers at this line
- Type mismatch: cannot convert from long to int
- The method sort(Comparator<? super Message>) in the type List<Message> is not applicable for the arguments ((Message o1, Message o2)
-> {})
最佳答案
Comparator#compareTo
返回一个 int
;而 getTime
显然是 long
.
这样写会更好:
.sort(Comparator.comparingLong(Message::getTime))
关于Java 8 lambda : Comparator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44225896/