当前位置:网站首页>Stream stream
Stream stream
2022-07-19 10:32:00 【Bai Xiaoyun】
Stream Use of streams
- Generative flow
Generate streams from data sources
- Intermediate operation
A stream can be followed by zero or more intermediate operations , The main purpose is to open the stream , Make some degree of data filtering mapping , Then return a new stream
- End operation
A stream can only have one termination operation , When this operation is performed , Stream is used ” light “ 了 , Cannot be operated on , So this must be the last operation of the flow forEach()
Stream Common ways to generate streams
Collection The collection of systems can generate streams using the default method
Map The set of systems generates streams indirectly
Arrays can be accessed through Stram Static methods of the interface of(T...vaues) Generative flow
//Colluction aggregate
ArrayList<String> arrayList=new ArrayList();
Stream stream = arrayList.stream();
//Map aggregate
Map<String, Integer> map=new HashMap<>();
Stream<String> stream1 = map.keySet().stream();
Stream<Integer> stream2 = map.values().stream();
// Array
String[] arrs={"hello","java","hi"};
Stream<String> arrs1 = Stream.of(arrs);
filter Filtering operation
//Colluction aggregate
ArrayList<String> arrayList=new ArrayList();
arrayList.add(" Xiao Ming Wang ");
arrayList.add(" Zhang Xiaohong ");
arrayList.add(" Li Dali ");
arrayList.add(" Wang Ming ");
// Start with a sheet , And the length is greater than or equal to 3
arrayList.stream().filter((String s)->{ return s.startsWith(" king ");}).filter((String s)->{return s.length()>=3;}).forEach(System.out::println);
![]()
Stream In the middle operation limit&skip
limit: Take the first few elements to form a stream
skip: Skip several elements to form a stream
//Colluction aggregate
ArrayList<String> arrayList=new ArrayList();
arrayList.add(" Xiao Ming Wang ");
arrayList.add(" Zhang Xiaohong ");
arrayList.add(" Li Dali ");
arrayList.add(" Wang Ming ");
// Take the first three data and output them on the console
arrayList.stream().limit(3).forEach(System.out::println);
System.out.println("--------------");
// Skip three elements , Put the rest of the elements in the console output
arrayList.stream().skip(3).forEach(System.out::println);

stream Flow intermediate operation concat&distinct
concat: Merge two streams into one stream
distinct: Returns a stream consisting of different elements of the stream
//Colluction aggregate
ArrayList<String> arrayList=new ArrayList();
arrayList.add(" Xiao Ming Wang ");
arrayList.add(" Zhang Xiaohong ");
arrayList.add(" Li Dali ");
arrayList.add(" Wang Ming ");
// Take the first three to form a stream
Stream<String> limit = arrayList.stream().limit(3);
// Take the last two to form a stream
Stream<String> skip = arrayList.stream().skip(2);
// Merge these two stream outputs
// Stream.concat(limit,skip).forEach(System.out::println);
System.out.println("------------------");
// Eliminate duplicate elements
Stream.concat(limit,skip).distinct().forEach(System.out::println);

stream Flow intermediate operation sorted&sorted(Comparator comparator)
public static void main(String[] args) {
//Colluction aggregate
ArrayList<String> arrayList=new ArrayList();
arrayList.add("zhangsan");
arrayList.add("lisi");
arrayList.add("wangwu");
arrayList.add("zhaoliu");
// Output the data on the console in alphabetical order
// arrayList.stream().sorted().forEach(System.out::println);
// Output the data on the console according to the string length , And when the length is the same , In natural order
arrayList.stream().sorted((o1,o2)->{int num1= o1.length()-o2.length();
int num2=num1==0?o1.compareTo(o2):0;
return num2;
}).forEach(System.out::println);
stream Flow intermediate operation map&mapToInt
//map Returns a stream consisting of the results of the elements that the given function applies to this flow
//mapToInt: Return to one IntStream It contains... That will be given Function applies the element result of this flow
//arrayList.stream().map(Integer::parseInt).forEach(System.out::println);
arrayList.stream().mapToInt(Integer::parseInt).forEach(System.out::println);
IntStream There is one of them. sum() The method of summation
//Colluction aggregate
ArrayList<String> arrayList=new ArrayList();
arrayList.add("10");
arrayList.add("20");
arrayList.add("30");
arrayList.add("40");
int sum = arrayList.stream().mapToInt(Integer::parseInt).sum();
System.out.println(sum);

Stream Common termination methods for streams
forEach()
count: There are several elements in the statistical set
//Colluction aggregate
ArrayList<String> arrayList=new ArrayList();
arrayList.add("10");
arrayList.add("20");
arrayList.add("30");
arrayList.add("40");
long count = arrayList.stream().count();
System.out.println(count);![]()
边栏推荐
- R language uses the ordinal of epidisplay package or. The display function obtains the summary statistical information of the ordered logistic regression model (the odds ratio and its confidence inter
- 中科磐云——网络空间安全抓包题目 B.pcap
- Software engineering - ranking of majors in Chinese Universities of Software Science
- ty_ Gr551x code framework
- 2022 Shaanxi secondary vocational group "Cyberspace Security" - packet analysis
- String类型函数传递问题
- 私钥,公钥的区分——私钥公钥讲解
- 如何解决谷歌浏览器解决跨域访问的问题
- Quick completion guide of manipulator (zero five): resources related to manipulator
- 旋转矩阵(Rotate Matrix)的性质分析(转发)
猜你喜欢

HCIA 静态基础实验 7.8

【牛客刷题】/*C语言实现字符串左旋*/

Figure an introduction to the interpretable method of neural network and a code example of gnnexplainer interpreting prediction

查找——平衡二叉树

如何解决谷歌浏览器解决跨域访问的问题

如何在双链笔记软件中建立仪表盘和知识库?以嵌入式小组件库 NotionPet 为例

快速判断站点是否存活的 3 种编程实现

Feature level fusion in Bev space

HCIP 第一天 7.15

C# SerialPort配置和属性了解
随机推荐
R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、设置na.rm参数为FALSE之后包含缺失值的分组的统计量的结果为NA
Studio 3T unlimited trial
STM32F407 NVIC
String类型函数传递问题
c# treeView 树形结构递归处理(企业集团型层次树形展示)
The R language uses the plot function in the native package (basic import package, graphics) to visualize the scatter plot
HCIA static basic experiment 7.8
R language ggplot2 visualization: use the gghistogram function of ggpubr package to visualize the grouping histogram, and use the palette parameter to customize the bar border color of the grouping hi
爱可可AI前沿推介(7.17)
【PostgreSQL 】PostgreSQL 15对distinct的优化
架构实战营|模块7
Go to school = earn money? Immortal college without paying tuition fees!
选择比努力更重要
知其然,而知其所以然,JS 对象创建与继承
如何在双链笔记软件中建立仪表盘和知识库?以嵌入式小组件库 NotionPet 为例
C# SerialPort配置和属性了解
NJCTF 2017messager
BEV空间内的特征级融合
R语言dplyr包select函数删除dataframe数据中包含指定字符串内容的数据列(drop columns in dataframe)
[Northeast Normal University] information sharing of postgraduate entrance examination and re examination