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 ...
随机推荐
- docker环境下elasticsearch安装ik和拼音分词
elasticsearch拼音分词地址:https://github.com/medcl/elasticsearch-analysis-pinyin/releases 在elasticsearch下面 ...
- Celery提交任务出错?
跟着官方的入门教程部署和运行的,为啥报这个错? tasks.py # -*- encoding:UTF-8 -*- from celery import Celery brokers = 'redis ...
- JavaScript复制文本探究
JS复制文本基本分为两步-First: 选中需要复制的节点,及选区:Second: 执行document.execCommand('copy')命令复制 对于选区,属于HTMLInputElement ...
- 从Uber微服务看最佳实践如何炼成?
导读:Uber成长非常迅速,工程师团队快速扩充,据说Uber有2000名工程师,8000个代码仓库,部署了1000多个微服务.微服务架构是Uber应对技术团队快速增长,功能快速上线很出色的解决方案.本 ...
- SLAM学习--开源测试数据集合
Tum RGB-D SLAM Dataset and Benchmark https://vision.in.tum.de/data/datasets/rgbd-dataset Kitti http: ...
- Aspnet Core 对 Resetful API版本的支持
在实际项目过程中API往往会收到迭代的影响,同时具备多个版本,因此resetful接口的版本话是非常重要的. 其实官方就提供了很好的支持,微软爸爸在nuget提供了Microsoft.AspNetCo ...
- html_之css
css 有三种形式的写法: 直接在标签里写入style样式 在<head></head>里写入<style></style>样式 直接创建.css 文件 ...
- 洛谷 p2196 挖地雷 题解
好久没有写博客了,今天水几篇博客 传送门 挖地雷这个题之前在 信息学奥赛一本通 上做过几乎一样的题,但是由于数据太水导致我当时过了,进而导致我昨天(4.28)考试丢了20分,今天写一篇题解 这个挖 ...
- UOJ#440. 【NOIP2018】填数游戏 动态规划
原文链接www.cnblogs.com/zhouzhendong/p/UOJ440.html 前言 菜鸡选手到省选了才做联赛题. 题解 首先我们分析一下性质: 1. 假如一个格子是 0,那么它的右上角 ...
- 相机标定问题-Matlab & Py-Opencv
一.相机标定基本理论 1.相机成像系统介绍 图中总共有4个坐标系: 图像坐标系:Op 坐标表示方法(u,v) Unit:Dots(个) 成像坐标系:Oi ...