当前位置:网站首页>List filtering, sorting, verification and other processing methods
List filtering, sorting, verification and other processing methods
2022-06-11 02:55:00 【bin_ d】
List filter: Filter 、map: mapping 、sorted: Sort 、forEach、collect: polymerization 、statistics: Statistics 、parallelStream: Parallel flow
public class Example{
private String name;
private Double score;
public Example(String name, Double score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
@Override
public String toString() {
return "Example{" +
"name='" + name + '\'' +
", score=" + score +
'}';
}
}
+--------------------+ +------+ +------+ +---+ +-------+
| stream of elements +-----> |filter+-> |sorted+-> |map+-> |collect|
+--------------------+ +------+ +------+ +---+ +-------+
Processing test classes
import java.util.ArrayList;
import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String args[]){
List<Example> list = new ArrayList<>();
list.add(new Example(" Xiao feng ", 20.d));
list.add(new Example(" False bamboo ", 40.d));
list.add(new Example(" Duan Yu ", 80.d));
list.add(new Example(" Sweeping monk ", 50.d));
list.add(new Example(" Buddhist abbot ", null));
list.add(new Example(" Wang Yu Yan ", 30.d));
long count = 0;
List<Example> filterList = null;
// filter Use of filters
// Select the number of students whose grades are not empty
count = list.stream().filter(p -> null != p.getScore()).count();
System.out.println(" The number of students taking the exam :" + count);
// collect
// Filter out the student set whose score is not empty
filterList = list.stream().filter(p -> null != p.getScore()).collect(Collectors.toList());
System.out.println(" Information about students taking the exam :");
filterList.stream().forEach(System.out::println);
// map Map a set to another set
// Take out all the students' grades
List<Double> scoreList = list.stream().map(p -> p.getScore()).collect(Collectors.toList());
System.out.println(" All students' scores are collected :" + scoreList);
// String the student name set into a string , Separate with commas
String nameString = list.stream().map(p -> p.getName()).collect(Collectors.joining(","));
System.out.println(" All student name strings :" + nameString);
// sorted Sort
// Sort students' grades in reverse order Positive order does not require addition .reversed()
filterList = list.stream().filter(p -> null != p.getScore()).sorted(Comparator.comparing(UserPo::getScore).reversed()).collect(Collectors.toList());
System.out.println(" All students' scores are collected , Reverse order :");
filterList.stream().forEach(System.out::println);
System.out.println(" Collect according to students' grades :");
Map<Double, List<UserPo>> groupByScoreMap = list.stream().filter(p -> null != p.getScore())
.collect(Collectors.groupingBy(UserPo::getScore));
for (Map.Entry<Double, List<UserPo>> entry : groupByScoreMap.entrySet()) {
System.out.println(" achievement :" + entry.getKey() + " The number of :" + entry.getValue().size());
}
// forEach
filterList.stream().forEach(p -> p.setScore(p.getScore() + 10));
System.out.println(" There are too few qualified people , Add to everyone 10 branch ");
filterList.stream().forEach(System.out::println);
// count
count = filterList.stream().filter(p -> p.getScore() >= 60).count();
System.out.println(" The number of people who have passed the final exam " + count);
DoubleSummaryStatistics statistics = filterList.stream().mapToDouble(p -> p.getScore()).summaryStatistics();
System.out.println(" The largest number in the list : " + statistics.getMax());
System.out.println(" The smallest number in the list : " + statistics.getMin());
System.out.println(" Sum of all Numbers : " + statistics.getSum());
System.out.println(" The average : " + statistics.getAverage());
// Parallel flow Use
count = list.parallelStream().filter(p -> null != p.getScore()).count();
System.out.println("test:" + count);
}
}边栏推荐
- 【斐波那契数列】
- Arduino Uno接JQ8900-16p语音播报模块
- CPT 102_ LEC 13-14
- Rs232/rs485 to 4G DTU uploading temperature and humidity sensor data based on Modbus protocol to remote TCP server
- 92. actual combat of completable future
- How can Delma's own brand "take off" when Philips is listed on the market?
- 【189. 轮转数组】
- Baidu submits sitemap to prompt the solution of "index type is not handled"
- 逃离大城市的年轻人:扛住了房价和压力,没扛住流行病
- AOSP ~ WIFI默认开启 + GPS默认关闭 + 蓝牙默认关闭 + 旋转屏幕关闭
猜你喜欢

弄懂了采矿业与碳中和的逻辑,就读懂了矿区无人驾驶的千亿市场

那些笑着离开“北上广”的人,为何最后都哭了?

Byte beating | the first batch of written examination for game R & D post (question solution)
![【长时间序列预测】Aotoformer 代码详解之[3]模型整体架构分析](/img/77/30215c363ae8a1324db1cbcaa5324b.png)
【长时间序列预测】Aotoformer 代码详解之[3]模型整体架构分析

How to use phpMyAdmin to optimize MySQL database

Graphacademy course explanation: Fundamentals of neo4j graph data science

CocosCreator原生二次开发的正确姿势

Arduino uses nRF24L01 module for wireless communication

Flat data to tree and tree data flattening

从绿联冲刺IPO,看手机配件市场沉浮录
随机推荐
Kotlin apply method
If you understand the logic of mining and carbon neutrality, you will understand the 100 billion market of driverless mining areas
AOSP - Developer mode is enabled by default
【新晋开源项目】动态配置化任务编排框架 Gobrs-Async 加入Dromara开源社区
WordPress upgrade error: briefly unavailable for scheduled maintenance [resolved]
【面试题 17.04. 消失的数字】
Go 语言的优势和学习路线图
Databinding escaping with presentation symbols
学习太极创客 — ESP8226 (二)
位置数据融合表3
重磅直播!ORB-SLAM3系列之特征匹配(MLPnP、词袋模型等)。
Google Gmail mailbox marks all unread messages as read at once
Problèmes de classe d'outils JDBC
Limiting visibility of symbols when linking shared libraries
【斐波那契数列】
Common vocabulary of software testing English
[long time series prediction] aotoformer code detailed [3] model overall architecture analysis
Navicat premium 15 tool is automatically deleted by anti-virus protection software solution
微信模版消息errcode“:40165,“errmsg“:“invalid weapp pagepath
error exepected identifier before ‘(‘ token, grpc 枚举类编译错误