原文链接:http://www.concretepage.com/java/jdk-8/java-8-stream-sorted-example 
国外对Java8一系列总结的不错, 翻译过来给大家共享 
这篇文章将会讲解Java 8 Stream sorted()示例, 我们能够以自然序或着用Comparator 接口定义的排序规则来排序一个流。Comparator 能用用lambada表达式来初始化, 我们还能够逆序一个已经排序的流。 
接下来我们将会使用java 8 的流式sorted排序List 、Map 、 Set 
1、sorted() 默认使用自然序排序, 其中的元素必须实现Comparable 接口 
2、sorted(Comparator<? super T> comparator) :我们可以使用lambada 来创建一个Comparator 实例。可以按照升序或着降序来排序元素。 
下面代码以自然序排序一个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()) 

Stream sorted() with List

我们排序一组装着Student 类对象的List 集合。 首先我们使用自然序, 接着我们使用Comparator 分别进行升序和降序: 
SortList.java

package com.concretepage;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortList {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student(1, "Mahesh", 12));
list.add(new Student(2, "Suresh", 15));
list.add(new Student(3, "Nilesh", 10)); System.out.println("---Natural Sorting by Name---");
List<Student> slist = list.stream().sorted().collect(Collectors.toList());
slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge())); System.out.println("---Natural Sorting by Name in reverse order---");
slist = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge())); System.out.println("---Sorting using Comparator by Age---");
slist = list.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge())); System.out.println("---Sorting using Comparator by Age with reverse order---");
slist = list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());
slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
}
}

* Student.java *

package com.concretepage;
public class Student implements Comparable<Student> {
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public int compareTo(Student ob) {
return name.compareTo(ob.getName());
}
@Override
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
final Student std = (Student) obj;
if (this == std) {
return true;
} else {
return (this.name.equals(std.name) && (this.age == std.age));
}
}
@Override
public int hashCode() {
int hashno = 7;
hashno = 13 * hashno + (name == null ? 0 : name.hashCode());
return hashno;
}
}

* Output *

---Natural Sorting by Name---
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10
Id:2, Name: Suresh, Age:15
---Natural Sorting by Name in reverse order---
Id:2, Name: Suresh, Age:15
Id:3, Name: Nilesh, Age:10
Id:1, Name: Mahesh, Age:12
---Sorting using Comparator by Age---
Id:3, Name: Nilesh, Age:10
Id:1, Name: Mahesh, Age:12
Id:2, Name: Suresh, Age:15
---Sorting using Comparator by Age with reverse order---
Id:2, Name: Suresh, Age:15
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10

JDK 8 之 Stream sorted() 示例的更多相关文章

  1. JAVA8 之 Stream sorted() 示例

    下面代码以自然序排序一个listlist.stream().sorted() 自然序逆序元素,使用Comparator 提供的reverseOrder() 方法list.stream().sorted ...

  2. Java8 使用 stream().sorted()对List集合进行排序

    集合对像定义 集合对象以学生类(StudentInfo)为例,有学生的基本信息,包括:姓名,性别,年龄,身高,生日几项. 使用stream().sorted()进行排序,需要该类实现 Comparab ...

  3. java8 stream sorted

    1.对象类型配列 List<Person> list = Arrays.asList( new Person(22, "shaomch", "man" ...

  4. JDK 8 中Stream流中的去重的方法

    JDK 8 中Stream流中去重的方法 1.简单的去重,可以使用distinct()方法去重,该方法是通过比较equals和hashcode值去去重, 2.复杂的去重, 例如,在一个JavaBean ...

  5. JDK新特性——Stream代码简洁之道的详细用法

    一.概述 Stream 是一组用来处理数组.集合的API,Stream API 提供了一种高效且易于使用的处理数据的方式. Java 8 中之所以费这么大的功夫引入 函数式编程 ,原因有两个: 代码简 ...

  6. 【JDK8】JDK 8 中Stream流中的去重的方法

    JDK 8 中Stream流中去重的方法 1.简单的去重,可以使用distinct()方法去重,该方法是通过比较equals和hashcode值去去重, 2.复杂的去重, 例如,在一个JavaBean ...

  7. java8 Stream sorted()的一次调用链记录

    代码 public static void main (String[] args) { Stream.of("d2", "a2", "b1" ...

  8. stream.map示例

    引用1:https://blog.csdn.net/sanchan/article/details/70753645 引用2:https://www.ibm.com/developerworks/cn ...

  9. Jdk提供的动态代理示例

    package com.jiaoyiping.util.demo; import java.lang.reflect.InvocationHandler; import java.lang.refle ...

随机推荐

  1. filp_open/filp_close/vfs_read/vfs_write

    Linux系统成功的关键因素之一就是具有与其他操作系统和谐共存的能力.Linux系统的文件系统由两层结构构建:第一层是虚拟文件系统(VFS),第二层是各种不同的具体的文件系统. VFS就是把各种具体的 ...

  2. 网页常用Js代码

    1.后退前进 <input type="button" value="后退" onClick="history.go(-1)"> ...

  3. python-装饰器&生成器&迭代器&推导式

    一:普通装饰器 概念:在不改变原函数内部代码的基础上,在函数执行之前和之后自动执行某个功能,为已存在的对象添加某个功能 普通装饰器编写的格式 def 外层函数(参数) def 内层函数(*args,* ...

  4. spring boot 2.0 配置双数据源 MySQL 和 SqlServer

    参考:https://www.cnblogs.com/xiaofengfeng/p/9552816.html 安装 org.mybatis.spring.boot:mybatis-spring-boo ...

  5. 【转】【Android】1分钟不用改任何代码在Eclipse中使用AAR

    原文:https://www.jianshu.com/p/ccf306e08d5b?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=t ...

  6. Linux NFS服务器的安装与配置方法(图文详解)

    这篇文章主要介绍了Linux NFS服务器的安装与配置方法(图文详解),需要的朋友可以参考下(http://xb.xcjl0834.com) 一.NFS服务简介 NFS 是Network File S ...

  7. sklearn库 线性回归库 LinearRegression

    import numpy as np import sklearn.datasets #加载原数据 from sklearn.model_selection import train_test_spl ...

  8. gcd前缀和-蒜头君的数轴

    题目: 今天蒜头君拿到了一个数轴,上边有 n个点,但是蒜头君嫌这根数轴不够优美,想要通过加一些点让它变优美,所谓优美是指考虑相邻两个点的距离,最多只有一对点的距离与其它的不同. 蒜头君想知道,他最少需 ...

  9. Xcode 10.1 运行老版本工程遇到问题解决记录

    近来接手公司一个历史遗留项目进行修改.上线工作,想想我都近三年没敲过iOS代码了也是慌.. 项目大致情况: 18年年初应上线项目,各种情况下一直搁置,到18年底了要重启上线,原来开发人员离职了都,年底 ...

  10. scrapy的持久化相关

    终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作. 需求是:将糗百首页中段子的内容和标 ...