在Java 8中按可比Bean属性排序 [英] Sort by comparable Bean property in Java 8
本文介绍了在Java 8中按可比Bean属性排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用这种模式,使用Java 8流按可比较属性进行排序的方法是否更短?
Is there a shorter way to sort by a comparable property using Java 8 streams than this pattern?
collection.stream()
.sorted((a,b) -> a.getProp().compareTo(b.getProp()))
推荐答案
是的,您可以使用方法参考:
collection.stream().sorted(Comparator.comparing(MyClass::getProp));
这篇关于在Java 8中按可比Bean属性排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文