当前位置:网站首页>Stream learning record
Stream learning record
2022-06-26 13:00:00 【Learning notes of net fish】
One 、 summary
stream Flow is Java8 , which deals with key abstractions of collections , He can specify what you want to do with the collection , You can perform very complex lookups 、 Filtering and mapping operations . Use StreamAPI Operates on the collection data , It's kind of like using SQL Execute database query , You can also use StreamAPI To execute operations in parallel .
in short :StreamAPI Provides an efficient and easy-to-use way to process data .
characteristic :
- It's not a data structure , It doesn't save data
- The original data source will not be modified , It saves the data after the operation to another object
- Lazy evaluation , The stream is in the reconstruction process , It's just a record of the operation , Not immediately executed , The timing calculation will not be performed until the termination operation is executed
Two 、 Operation classification 
- No state : The processing of an element is not affected by the previous element
- A stateful : The operation cannot continue until all elements have been obtained
- Non short circuit operation : All elements must be processed to get the final result
- Short circuit operation : When you meet some elements that meet the conditions, you can get the final result , Such as A||B, as long as A by true, There is no need to judge B Result
3、 ... and 、 The specific use
- Common methods for creating streams
1.1 Use Collection Under the stream() and parallelStream() Method
List<AnalysisOrderCaseGroupDO> list = page.getList();
List<Long> caseGroupIds = list.stream().map(AnalysisOrderCaseGroupDO::getId)
.collect(Collectors.toList());
dataList.parallelStream().forEach(d -> geometryBaseService.doSimplify(d, zoom));
1.2 Use Arrays Medium stream() Method , Convert array to stream
Integer[] nums = new Integer[10];
Stream<Integer> stream = Array.stream(nums);
Here is an example used in enumerations :
public enum ModeEnum{
// File operation mode
READ_ONLY(1, " read-only "),
EDITABLE(2, " Editable "),
;
Integer code;
String desc;
public static String descOf(Integer code){
if(code == null ){
return "";
}
return Arrays.Stream(ModeEnum.values())
.filter(o -> Objects.equals(o.getCode(), code))
.findAny().map(ModeEnum::getDesc).orElse("");
}
}
1.3 Use Stream Static methods in :of()、iterator() 、generate()
A small case :
public class StreamOfTest {
public static void main(String[] args) {
String idStr = "12, 22, 33,44";
List<Long> ids = Stream.of(idStr.split(","))
.map(String::trim)
.map(Long::valueOf)
.collect(Collectors.toList());
System.out.println(ids);
Stream<Integer> stream2 = Stream.iterate(0, (x) -> x + 2).limit(6);
stream2.forEach(System.out::println);
Stream<Double> stream3 = Stream.generate(Math::random).limit(2);
stream3.forEach(System.out::println);
}
}
1.4 Use BufferReader.lines() Method , Turn each line into a stream
边栏推荐
- 中科软外包一面
- 机组实践实验8——使用CMStudio设计基于基本模型机微程序指令(1)
- Summary of some application research cases of UAV Remote Sensing in forest monitoring
- Wechat applet wx Request request encapsulation
- ES6:迭代器
- 无人机遥感在森林监测的部分应用研究案例总结
- Redis learning - 02 common data types, operation commands and expiration time
- 5+API,清除应用缓存
- 【网络是怎么连接的】第二章(下):一个网络包的接收
- 计组实践实验9——使用CMStudio设计基于分段模型机微程序指令(2)
猜你喜欢
C# const详解:C#常量的定义和使用
P5733 【深基6.例1】自动修正
A must for programmers, an artifact utools that can improve your work efficiency n times
NoSQL mongodb - 02 mongodb server installation, mongodb shell, basic concepts and visualization tools
NoSQL mongodb - 01 introduction to NoSQL and mongodb
Deeply analyze the differences between dangbei box B3, Tencent Aurora 5S and Xiaomi box 4S
goto语句实现关机小程序
环形队列php
Redis learning - 02 common data types, operation commands and expiration time
关于NaN的一些总结
随机推荐
Sharing ideas for a quick switch to an underlying implementation
.NET MAUI 性能提升
PHP unit conversion
初识-软件测试
自动化测试的局限性你知道吗?
7-2 摘花生
P2393 yyy loves Maths II
Laravel subdomain accesses different routing files and different modules
国标GB28181协议EasyGBS视频平台TCP主动模式拉流异常情况修复
7-1 数的范围
Several rare but useful JS techniques
【网络是怎么连接的】第二章(下):一个网络包的接收
7-16 货币系统Ⅰ
Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
power designer - 自定义注释按钮
Echart堆叠柱状图:色块之间添加白色间距效果设置
Vivado 错误代码 [DRC PDCN-2721] 解决
软件测试 - 概念篇
Xiaobai lazy special-win10-win11 one click installation version
一个初级多线程服务器模型