使用 Java Stream 实现集合排序
排序集合中的对象
1.源码介绍
1.1 Stream sorted()
源码查看:
/*** Returns a stream consisting of the elements of this stream, sorted* according to natural order. If the elements of this stream are not* {@code Comparable}, a {@code java.lang.ClassCastException} may be thrown* when the terminal operation is executed.** <p>For ordered streams, the sort is stable. For unordered streams, no* stability guarantees are made.** <p>This is a <a href="package-summary.html#StreamOps">stateful* intermediate operation</a>.** @return the new stream*/
Stream<T> sorted();
说明:T 必须是实现了 Comparable 接口的类,否则方法会抛出 ClassCastException 异常。
1.2. Stream sorted(Comparator<? super T> comparator)
源码查看:
/*** Returns a stream consisting of the elements of this stream, sorted* according to the provided {@code Comparator}.** <p>For ordered streams, the sort is stable. For unordered streams, no* stability guarantees are made.** <p>This is a <a href="package-summary.html#StreamOps">stateful* intermediate operation</a>.** @param comparator a <a href="package-summary.html#NonInterference">non-interfering</a>,* <a href="package-summary.html#Statelessness">stateless</a>* {@code Comparator} to be used to compare stream elements* @return the new stream*/
Stream<T> sorted(Comparator<? super T> comparator);
说明:根据给定的 比较器 进行排序。Comparator是一个函数式接口,其源码如下(仅展示关键部分):
@FunctionalInterface
public interface Comparator<T> {/*** Compares its two arguments for order. Returns a negative integer,* zero, or a positive integer as the first argument is less than, equal* to, or greater than the second.<p>* * ......(此处略去部分注释)** @param o1 the first object to be compared.* @param o2 the second object to be compared.* @return a negative integer, zero, or a positive integer as the* first argument is less than, equal to, or greater than the* second.* @throws NullPointerException if an argument is null and this* comparator does not permit null arguments* @throws ClassCastException if the arguments' types prevent them from* being compared by this comparator.*/int compare(T o1, T o2);
说明:方法的返回值分类以及含义如下:
- 负数:o1 小于 o2
- 0:o1 等于 o2
- 正数:o1 大于 o2
3. 使用
3.1 Stream sorted()
3.2 Stream sorted(Comparator<? super T> comparator)
使用 Java Stream 实现集合排序相关推荐
- Java stream().sorted()实现排序(升序、降序、多字段排序)
1 自然排序 sorted():自然排序,流中元素需实现Comparable接口 package com.entity;import lombok.*;@Data @ToString @AllArgs ...
- Java Stream 流集合去重排序
文章目录 1. list去重 2. 根据对象中的某个字段进行list去重 3. 排序 4. 排序并去重 1. list去重 List<String> list = new ArrayLis ...
- java中对集合排序,Java如何对集合中的项目排序?
整理一个诀窍java.util.Set是使用的执行java.util.SortedSet,如java.util.TreeSet类.下面的示例向您显示使用java.util.TreeSet该类的结果,其 ...
- java stream toarray_Java集合工具类的一些坑,Arrays.asList()、Collection.toArray()...
Arrays.asList() 使用指南 最近使用Arrays.asList()遇到了一些坑,然后在网上看到这篇文章:Java Array to List Examples 感觉挺不错的,但是还不是特 ...
- Java—stream以及集合框架使用
1) 编写Student类,主要属性包括学号.姓名.性别.班级 2) 编写Score类,主要属性包括:学号.课程名.分数 3) 模拟期末考试的成绩统计应用场景,要求 (1) 所有学生名单及对应科目成绩 ...
- Java日期型集合排序
代码 package com.qian.util;import java.text.DateFormat; import java.text.ParseException; import java.t ...
- java中list元素排序_java list集合元素根据某些字段排序
一.jdk1.6的环境下 新建ComparatorSort类,并实现Comparator接口,重写compare方法 降序排序:o1的元素>o2的元素,并返回-1:o1的元素小于o2的元素,并返 ...
- Java 8 集合排序的 10 个姿势
今天栈长就分享 Java 8 进行排序的 10 个姿势,原来还有这么多排序技巧,其实就是把 Java 8 中的 Lambda.Stream.方法引用等知识点串起来,栈长的同事直呼还看不懂.. 传统排序 ...
- java中集合排序的常用方法总结
前言 1.集合元素为数字 2.集合元素为对象 前言 平常的开发需求中肯定会遇到对集合排序问题,最常见的排序是在持久层中使用sql进行排序,但是由于业务限制或是其他原因,不能在持久层 ...
最新文章
- 空间简史-人类认识空间的旅程与其对强化学习的启示
- Android:展开/折叠动画
- MFC为什么要保留原来的CPen, 然后SelectObject
- ftruncate函数的功能及使用
- 【遥感物候】Matlab求解一元六次多项式,计算植被生长季始期
- python地理位置聚类_python – 用于聚类地理位置数据的DBSCAN
- Linux 添加新硬盘
- 大数据实时分析平台应用在哪些场景
- Android开发中的SQLite事务处理,即beginTransaction()方法
- 两种前端在线json编辑器方案(无法解决number精度丢失问题)
- 快速排序时间复杂度分析
- HDMI中的视频时序分析
- Go语言 defer 和 recover
- JAVA 创建文件和文件夹,删除文件和文件夹的实用工具(转载自-张长胜)
- wordpress pdf_9个适用于WordPress的最佳PDF插件
- 车规电阻AEC-Q200测试项目及元器件检测设备
- 基岩版刷铁傀儡机制和Java_我的世界:Java版1.14获得铁傀儡的四种方式,第三种千万别忘记...
- 很全面的5G技术基础知识PPT(三)
- Python语言学习笔记-001-Python 3.3.0的配置与Hello World
- 奶瓶仔xp主题【主题世界】