JDK 8 之 Stream sorted() 示例
原文链接: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() 示例的更多相关文章
- JAVA8 之 Stream sorted() 示例
下面代码以自然序排序一个listlist.stream().sorted() 自然序逆序元素,使用Comparator 提供的reverseOrder() 方法list.stream().sorted ...
- Java8 使用 stream().sorted()对List集合进行排序
集合对像定义 集合对象以学生类(StudentInfo)为例,有学生的基本信息,包括:姓名,性别,年龄,身高,生日几项. 使用stream().sorted()进行排序,需要该类实现 Comparab ...
- java8 stream sorted
1.对象类型配列 List<Person> list = Arrays.asList( new Person(22, "shaomch", "man" ...
- JDK 8 中Stream流中的去重的方法
JDK 8 中Stream流中去重的方法 1.简单的去重,可以使用distinct()方法去重,该方法是通过比较equals和hashcode值去去重, 2.复杂的去重, 例如,在一个JavaBean ...
- JDK新特性——Stream代码简洁之道的详细用法
一.概述 Stream 是一组用来处理数组.集合的API,Stream API 提供了一种高效且易于使用的处理数据的方式. Java 8 中之所以费这么大的功夫引入 函数式编程 ,原因有两个: 代码简 ...
- 【JDK8】JDK 8 中Stream流中的去重的方法
JDK 8 中Stream流中去重的方法 1.简单的去重,可以使用distinct()方法去重,该方法是通过比较equals和hashcode值去去重, 2.复杂的去重, 例如,在一个JavaBean ...
- java8 Stream sorted()的一次调用链记录
代码 public static void main (String[] args) { Stream.of("d2", "a2", "b1" ...
- stream.map示例
引用1:https://blog.csdn.net/sanchan/article/details/70753645 引用2:https://www.ibm.com/developerworks/cn ...
- Jdk提供的动态代理示例
package com.jiaoyiping.util.demo; import java.lang.reflect.InvocationHandler; import java.lang.refle ...
随机推荐
- neutron--ml2 plugin
ml2 plugin 对 plugin 的功能进行抽象和封装,有 ml2 plugin ,各种 network 无需开发自己的 plugin,只需开发 ml2 plugin 相对应的 driver , ...
- Linux Centos7.5从docker的安装到容器的部署运行
环境: Win10 内的 VMware workstation(Centos7 64位) 所有命令皆为 root 用户, 非 root 用户应在命令前加 sudo 查看系统版本命令: cat /etc ...
- 前言:JNI调用-简单使用
JNI JNI是(Java Native Interface 本地接口)是一个协议,用来沟通Java 代码和C/C++代码,是 Java和 C.C++之间的桥梁. 通过JNI协议,Java可以调用外 ...
- vapor 生成xcode project 产生的错误解决方式
运行vapor xcode时报错: Could not generate Xcode project: error: terminated(72): xcrun --sdk macosx --find ...
- .net基础学java系列(三)徘徊反思
.net基础学java系列(三)徘徊反思 上一篇文章:.net基础学java系列(二)IDE 之 插件 这两天晚上看完了IDEA的教学视频:https://edu.51cto.com/course/1 ...
- 「JOISC 2017 Day 3」幽深府邸
题解: 和hnoi2018day2t1基本一样 我想了半小时想出了一个很麻烦的做法 写了之后发现假掉了 刚开始想的是 先预处理出每个门要打开至少要在左边的哪个点$L[]$,右边的哪个点$R[]$ 对每 ...
- Linux下system()函数的实现
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types. ...
- day22.面向对象初识
1.面向对象引入 先来创建一个小游戏:人狗大战 # 定义一个狗 def Gog(name,blood,aggr,kind): dog = { 'name':name, 'blood':blood, ' ...
- 【HTTP】---HTTP状态码详解
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes 1.百科名片 HTTP状态码(HTTP Status Code)是用以表示网页服务器HT ...
- 基于Emgucv,C#的图片旋转方式
/// <summary> /// 图片旋转 --百度 旋转仿射 /// </summary> /// <param name="modelImage" ...