当前位置: 首页 > news >正文

Stream流的常用方法(自用)

自用的笔记, 有🚩 需要多看

基本数据

自定义实体

@Data
class Student{private String name;private Integer age;private Double height;public Student() {}
}

假数据

Student s1 = new Student();
s1.setAge(20);
s1.setName("cookie");
s1.setHeight(180d);Student s2 = new Student();
s2.setAge(30);
s2.setName("cookie");
s2.setHeight(180d);Student s3 = new Student();
s3.setAge(40);
s3.setName("bob");
s3.setHeight(175d);Student s4 = new Student();
s4.setAge(40);
s4.setName("bob");
s4.setHeight(180d);// 存入list集合
List<Student> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);

一, 分组

1. 一层分组/简单分组

/*** 需求一(一层分组):根据Age分组*/
System.out.println("需求一(一层分组):根据Age分组");
Map<Integer, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getAge));
for (Integer age : collect.keySet()) {System.out.println("key:" + age + "\tvalue:" + collect.get(age));
}/*** 控制台结果:* key:20	value:[Student(name=cookie, age=20, height=180.0)]* key:40	value:[Student(name=bob, age=40, height=175.0), Student(name=bob, age=40, height=180.0)]* key:30	value:[Student(name=cookie, age=30, height=180.0)]*/

2. 多层分组

/*** 需求二: 先根据name分组,然后再根据身高分组*/
System.out.println("需求二: 先根据name分组,然后再根据身高分组");
Map<String, Map<Double, List<Student>>> collect1 = list.stream().collect(Collectors.groupingBy(Student::getName, Collectors.groupingBy(Student::getHeight)));
Set<String> namesGroup = collect1.keySet();
for (String namekey : namesGroup) {Map<Double, List<Student>> heightGroupMap = collect1.get(namekey);Set<Double> height = heightGroupMap.keySet();for (Double h : height) {System.out.println("name:" + namekey + " height:" + heightGroupMap.get(h));}
}/*** 控制台结果:* name:bob height:[Student(name=bob, age=40, height=175.0)]* name:bob height:[Student(name=bob, age=40, height=180.0)]* name:cookie height:[Student(name=cookie, age=20, height=180.0), Student(name=cookie, age=30, height=180.0)]*/

3. 多层分组-自定义key 🚩

/*** 需求三: 自定义key返回 形式如下: age_height bob_175*/
System.out.println("需求三: 自定义key返回 形式如下: age_height bob_175");
Map<String, List<Student>> collect2 = list.stream().collect(Collectors.groupingBy(c -> c.getName() + "_" + c.getHeight()));for (String customKey : collect2.keySet()) {System.out.println("key:" + customKey +" value:"+ collect2.get(customKey));
}
/*** 控制台结果:* key:bob_180.0 value:[Student(name=bob, age=40, height=180.0)]* key:bob_175.0 value:[Student(name=bob, age=40, height=175.0)]* key:cookie_180.0 value:[Student(name=cookie, age=20, height=180.0), Student(name=cookie, age=30, height=180.0)]*/

二, 排序

方式一: 通过自定义的比较器(非必要不推荐)

/**
* 需求: 根据身高排序,如果身高相同,根据年龄排序,如果年龄依然相同,根据名称字母顺序排序
*/
List<Student> collect3 = list.stream().sorted(new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {// 这里前面的减去后面的是升序, 反之这是降序if (!o1.getHeight().equals(o2.getHeight())) {return (int) (o1.getHeight() - o2.getHeight());}if (!o1.getAge().equals(o2.getAge())) {return o1.getAge() - o2.getAge();}return o1.getName().compareTo(o2.getName());}
}).collect(Collectors.toList());
System.out.println(collect3);/*** 控制台结果:* [Student(name=bob, age=40, height=175.0), * Student(name=cookie, age=20, height=180.0), * Student(name=cookie, age=30, height=180.0), * Student(name=bob, age=40, height=180.0)]*/// 注: 当然上面的也可以做一个简化
List<Student> collect3 = list.stream().sorted((o1, o2) -> {// 这里前面的减去后面的是升序, 反之这是降序if (!o1.getHeight().equals(o2.getHeight())) {return (int) (o1.getHeight() - o2.getHeight());}if (!o1.getAge().equals(o2.getAge())) {return o1.getAge() - o2.getAge();}return o1.getName().compareTo(o2.getName());
}).collect(Collectors.toList());

方式二: 通过lambda 🚩

List<Student> collect4 = list.stream().sorted(Comparator.comparingDouble(Student::getHeight).thenComparingInt(Student::getAge).thenComparing(Student::getName)).collect(Collectors.toList());
System.out.println(collect4);/*** 控制台结果:* [Student(name=bob, age=40, height=175.0), * Student(name=cookie, age=20, height=180.0), * Student(name=cookie, age=30, height=180.0), * Student(name=bob, age=40, height=180.0)]*/// 注意:
// 方式一,升序降序是通过返回的正负, 
// 方式二而是通过方法, 现在我们首先通过身高降序, 我们只需要在条件的后面加一个reversed()后缀方法即可List<Student> collect4 = list.stream().sorted(Comparator.comparingDouble(Student::getHeight).reversed().thenComparingInt(Student::getAge).thenComparing(Student::getName)
).collect(Collectors.toList());
System.out.println(collect4);/*** 修改之后控制台结果:* [Student(name=cookie, age=20, height=180.0), * Student(name=cookie, age=30, height=180.0), * Student(name=bob, age=40, height=180.0), * Student(name=bob, age=40, height=175.0)]*/

三, 统计

/*** 需求: 统计年龄之和*/
int ageSum = list.stream().mapToInt(Student::getAge).sum();/*** 求年龄平均值*/
Double ageAvg1 = list.stream().collect(Collectors.averagingInt(Student::getAge));
// 或者
double ageAvg2 = list.stream().mapToInt(Student::getAge).average().getAsDouble();/*** 求年龄最大值*/
int maxAge = list.stream().mapToInt(Student::getAge).max().getAsInt();/*** 最小值*/
int minAge = list.stream().mapToInt(Student::getAge).min().getAsInt();

缓慢总结中~~~~

http://www.lryc.cn/news/170639.html

相关文章:

  • 【python函数】torch.nn.Embedding函数用法图解
  • with ldid... /opt/MonkeyDev/bin/md: line 326: ldid: command not found
  • [golang gui]fyne框架代码示例
  • 2000-2018年各省能源消费和碳排放数据
  • C# ref 学习1
  • MQ - 08 基础篇_消费者客户端SDK设计(下)
  • Flutter层对于Android 13存储权限的适配问题
  • Android kotlin开源项目-功能标题目录
  • Linux下,基于TCP与UDP协议,不同进程下单线程通信服务器
  • qt功能自己创作
  • Linux网络编程:使用UDP和TCP协议实现网络通信
  • 【后端速成 Vue】初识指令(上)
  • 爬虫 — Scrapy-Redis
  • tcpdump常用命令
  • 计算机网络运输层网络层补充
  • java CAS详解(深入源码剖析)
  • 1786_MTALAB代码生成把通用函数生成独立文件
  • 2023/09/19 qt day3
  • Docker 学习总结(78)—— Docker Rootless 让你的容器更安全
  • 如何使用ArcGIS Pro将等高线转DEM
  • 【爬虫基础】万字长文详解XPath
  • 分布式多级缓存SDK设计的思考
  • 设计模式:适配器模式(C++实现)
  • 【深度学习实验】前馈神经网络(二):使用PyTorch实现不同激活函数(logistic、tanh、relu、leaky_relu)
  • 容器技术所涉及Linux内核关键技术
  • IPV4和IPV6,公网IP和私有IP有什么区别?
  • 高云FPGA系列教程(7):ARM GPIO外部中断
  • Python爬虫:动态获取页面
  • 大数据平台迁移后yarn连接zookeeper 异常分析
  • Ubuntu Nginx 配置 SSL 证书