当前位置:网站首页>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);
}
}边栏推荐
- Will your company choose to develop data center?
- 剑指 Offer II 079. 所有子集
- Stc8a8k64d4 EEPROM read / write failure
- 那些笑着离开“北上广”的人,为何最后都哭了?
- Sword finger offer II 079 All subsets
- Istio安装与使用
- Prophet
- GraphAcademy 课程讲解:《Neo4j 图数据科学基础》
- 微信模版消息errcode“:40165,“errmsg“:“invalid weapp pagepath
- 【新晋开源项目】动态配置化任务编排框架 Gobrs-Async 加入Dromara开源社区
猜你喜欢

【189. 轮转数组】

Manon's advanced road - Daily anecdotes

Rs232/rs485 to 4G DTU uploading temperature and humidity sensor data based on Modbus protocol to remote TCP server

Baidu submits sitemap to prompt the solution of "index type is not handled"

The two request sessionids of the same user are inconsistent ----- record the problem

GraphAcademy 課程講解:《Neo4j 圖數據科學基礎》

If you understand the logic of mining and carbon neutrality, you will understand the 100 billion market of driverless mining areas

App test_ Summary of test points

HUST Software Engineering (Experiment 2) -- TDD test driven development experiment.

Arduino使用NRF24L01模块进行无线通信
随机推荐
Prophet
jdbc工具类的问题
AOSP - Developer mode is enabled by default
Can Xiaoxiang life become the "Yonghui" in the discount industry after the completion of the round a financing of tens of millions of yuan?
求MySQL先按大于等于当前时间升序排序,再按小于当前时间降序排序
Jetpack compose scaffold and topappbar (top navigation)
Byte beating | the first batch of written examination for game R & D post (question solution)
Openjudge noi 1.13 17: text layout
【面试题 17.04. 消失的数字】
Go语言之Go 快速入门篇(一):第一个 Go 程序
MySQL is required to sort in ascending order greater than or equal to the current time, and then in descending order less than the current time
Construction of Flink development environment and wordcount
The two departments jointly issued the nine provisions on fire safety management of off campus training institutions
The Google search console webmaster tool cannot read the sitemap?
How to add two factor authentication for WordPress websites
If you understand the logic of mining and carbon neutrality, you will understand the 100 billion market of driverless mining areas
OpenJudge NOI 1.13 17:文字排版
Application of the remote acquisition IOT gateway of the Bashir trough flowmeter in open channel flow monitoring
GraphAcademy 課程講解:《Neo4j 圖數據科學基礎》
CPT 102_LEC 20