关于JDK8中stream的用法小总结。
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<Student> studentList = new ArrayList<>(); studentList.add(new Student(1,2,1000,88));
studentList.add(new Student(2,2,1000,76));
studentList.add(new Student(3,1,2000,79));
studentList.add(new Student(4,2,3000,88));
studentList.add(new Student(5,2,3000,49));
studentList.add(new Student(5,1,4000,69));
studentList.add(new Student(5,1,4000,96));
// 按照班级分组,性别为女,分数降序 Map<String, List<Student>> groupList = new HashMap<String, List<Student>>(); studentList.stream().filter(student ->
student.getSex() == 2
).collect(Collectors.groupingBy(Student::getClassNo,
Collectors.toList())).forEach(
(classNo, listByClassNo) -> {
groupList.put(classNo + "",
listByClassNo.stream().sorted(Comparator.comparing(Student::getScore).reversed()).collect(Collectors.toList()));
} ); System.out.println("Hello World!");
} private static class Student implements Serializable { private int id; private int sex; private int classNo; private int score; public Student(int id, int sex, int classNo, int score) { this.id = id; this.sex = sex; this.classNo = classNo; this.score = score; } public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public int getSex() {
return sex;
} public void setSex(int sex) {
this.sex = sex;
} public int getClassNo() {
return classNo;
} public void setClassNo(int classNo) {
this.classNo = classNo;
} public int getScore() {
return score;
} public void setScore(int score) {
this.score = score;
}
} }
关于JDK8中stream的用法小总结。的更多相关文章
- JDK8中Stream使用解析
JDK8中Stream使用解析 现在谈及JDK8的新特新,已经说不上新了.本篇介绍的就是Stream和Lambda,说的Stream可不是JDK中的IO流,这里的Stream指的是处理集合的抽象概念『 ...
- Jdk8中Stream流的使用,让你脱离for循环
学习要求: 知道一点儿函数式接口和Lambda表达式的基础知识,有利于更好的学习. 1.先体验一下Stream的好处 需求:给你一个ArrayList用来保存学生的成绩,让你打印出其中大于60的成绩. ...
- jdk8中的stream
https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/ Java 8 中的 Streams API 详解 Streams 的背景 ...
- forEach与jdk8中的lambda, Stream
增强for循环 :forEach 反编译后可以看到实际使用的仍然是Iterator+while遍历的 forEach的优点是写法简单,缺点是不能使用xxx.remove(e)或者iter.remove ...
- java stream中Collectors的用法
目录 简介 Collectors.toList() Collectors.toSet() Collectors.toCollection() Collectors.toMap() Collectors ...
- 【JDK8】JDK 8 中Stream流中的去重的方法
JDK 8 中Stream流中去重的方法 1.简单的去重,可以使用distinct()方法去重,该方法是通过比较equals和hashcode值去去重, 2.复杂的去重, 例如,在一个JavaBean ...
- 13万字详细分析JDK中Stream的实现原理
前提 Stream是JDK1.8中首次引入的,距今已经过去了接近8年时间(JDK1.8正式版是2013年底发布的).Stream的引入一方面极大地简化了某些开发场景,另一方面也可能降低了编码的可读性( ...
- Linux中find常见用法
Linux中find常见用法示例 ·find path -option [ -print ] [ -exec -ok command ] {} \; find命令的参数 ...
- [转]Linux中find常见用法示例
Linux中find常见用法示例[转]·find path -option [ -print ] [ -exec -ok command ] {} \;find命令的参 ...
随机推荐
- 从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
MyDisruptor V6版本介绍 在v5版本的MyDisruptor实现DSL风格的API后.按照计划,v6版本的MyDisruptor作为最后一个版本,需要对MyDisruptor进行最终的一些 ...
- ZJOI2016 小星星 题解
我一生之敌是状压 本文发表于 洛谷博客:https://www.luogu.com.cn/blog/LoveMC/solution-p3349 Cnblogs:https://www.cnblogs. ...
- 基于Python3(Autosub)以及Ffmpeg配合GoogleTranslation(谷歌)为你的影片实现双语版字幕(逐字稿)
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_169 为影片加字幕其实是一件非常耗费时间的事情,尤其是对于打字慢的朋友来说.当然不光为影片加字幕,在其他领域,类似的逐字稿也是工作 ...
- 6.15 NOI 模拟
\(T1\ ckr\)与平方数 不会吧,不会吧,真有人不会积分,好吧,我真的一点也不会... 基本公式\(:\) \(1.\)多项式定积分的计算方法 \[f(x)=\sum_{i=0}^nc_ix^i ...
- 羽夏看Linux内核——启动那些事
写在前面 此系列是本人一个字一个字码出来的,包括示例和实验截图.如有好的建议,欢迎反馈.码字不易,如果本篇文章有帮助你的,如有闲钱,可以打赏支持我的创作.如想转载,请把我的转载信息附在文章后面,并 ...
- Apache DolphinScheduler 3.0.0 正式版发布!
点亮 ️ Star · 照亮开源之路 GitHub:https://github.com/apache/dolphinscheduler 版本发布 2022/8/10 2022 年 8 ...
- k8s驱逐篇(2)-kubelet节点压力驱逐
kubelet节点压力驱逐 kubelet监控集群节点的 CPU.内存.磁盘空间和文件系统的inode 等资源,根据kubelet启动参数中的驱逐策略配置,当这些资源中的一个或者多个达到特定的消耗水平 ...
- LuoguP2876 [USACO07JAN]解决问题Problem Solving (区间DP)(未完成)
#include "Head.cpp" const int N = 307; int f[N][N], a[N], b[N], sumA[N], sumB[N]; int main ...
- 对于Java中的Loop或For-each,哪个更快
Which is Faster For Loop or For-each in Java 对于Java中的Loop或Foreach,哪个更快 通过本文,您可以了解一些集合遍历技巧. Java遍历集合有 ...
- Druid学习之查询语法
写在前面 最近一段时间都在做druid实时数据查询的工作,本文简单将官网上的英文文档加上自己的理解翻译成中文,同时将自己遇到的问题及解决方法list下,防止遗忘. 本文的demo示例均来源于官网. D ...