Java的8 LAMBDA:比较

Nuñito德拉卡尔萨达:

我想排序和λ列表:

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))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章