我是靠谱客的博主 震动百合,这篇文章主要介绍java8笔记: sorted()之正序倒序,现在分享给大家,希望可以做个参考。

这篇文章将会讲解Java 8 Stream sorted()示例

下面代码以自然序排序一个list

list.stream().sorted() 

 

自然序逆序元素,使用Comparator 提供的reverseOrder() 方法

list.stream().sorted(Comparator.reverseOrder()) 

 

使用Comparator 来排序一个list

list.stream().sorted(Comparator.comparing(Student::getAge)) 

 

把上面的元素逆序

list.stream().sorted(Comparator.comparing(Student::getAge).reversed()) 

 

当然还可以不用借助steam方式直接排序:

list.sort(Comparator.comparing(Integer::intValue));

list.sort(Comparator.comparing(Integer::intValue).reversed());

list.sort(Comparator.comparing(Student::getAge));

list.sort(Comparator.comparing(Student::getAge).reversed());

 

转载于:https://www.cnblogs.com/woyaobianfei/p/9187127.html

最后

以上就是震动百合最近收集整理的关于java8笔记: sorted()之正序倒序的全部内容,更多相关java8笔记:内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部