当前位置:网站首页>Stream流学习记录
Stream流学习记录
2022-06-26 12:35:00 【网小鱼的学习笔记】
一、 概述
stream流是Java8中处理集合的关键抽象概念,他可以指定你希望对集合进行的操作,可以执行非常复杂的查找、过滤和映射操作。使用StreamAPI对集合数据进行操作,就类似于使用SQL执行数据库查询,也可以使用StreamAPI来并行执行操作。
简言之:StreamAPI提供了一种高效且易于使用的处理数据的方式。
特点:
- 不是数据结构,不会保存数据
- 不会修改原来的数据源,它会将操作后的数据保存到另一个对象中
- 惰性求值,流在重建处理过程中,只是对操作进行了记录,并不会立即执行,需要等到执行终止操作的时候才会进行时机的计算
二、操作分类
- 无状态:指元素的处理不受之前元素的影响
- 有状态:指该操作只有拿到所有元素之后才能继续下去
- 非短路操作:指必须处理所有元素才能得到最终结果
- 短路操作:指遇到某些符合条件的元素就可以得到最终结果,如A||B,只要A为true,则无需判断B的结果
三、具体用法
- 流的常用创建方法
1.1 使用Collection下的stream()和parallelStream()方法
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 使用Arrays中的stream()方法,将数组转化成流
Integer[] nums = new Integer[10];
Stream<Integer> stream = Array.stream(nums);
这里有一个在枚举中使用的例子:
public enum ModeEnum{
//文件操作方式
READ_ONLY(1, "只读"),
EDITABLE(2, "可编辑"),
;
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 使用Stream中的静态方法:of()、iterator() 、generate()
一个小案例:
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 使用BufferReader.lines()方法,将每行内容转化成流
边栏推荐
- 程序员必备,一款让你提高工作效率N倍的神器uTools
- Lodash common methods (filtering, anti shake...)
- [probability theory] conditional probability, Bayesian formula, correlation coefficient, central limit theorem, parameter estimation, hypothesis test
- Scala-day01- companion objects and HelloWorld
- VMware虚拟机 桥接模式 无法上网 校园网「建议收藏」
- power designer - 自定义注释按钮
- Configuring Apache digest authentication
- Analysis report on the "fourteenth five year plan" and investment prospect of China's pharmaceutical equipment industry 2022-2028
- Several methods added to the ES6 array (foreach, filter, some, every. Includes, reduce)
- Ad - update the modified PCB package to the current PCB
猜你喜欢

Scala-day06- pattern matching - Generic

The laravel dingo API returns a custom error message

不到40行代码手撸一个BlocProvider

深入解析 MySQL binlog

Vscode solves the problem of Chinese garbled code
![[solved] data duplication or data loss after laravel paginate() paging](/img/68/7bf51bbf893a91bee24f5f7d4a369f.jpg)
[solved] data duplication or data loss after laravel paginate() paging

由错误<note: candidate expects 1 argument, 0 provided>引发的思考

小白懒人专用-win10-win11一键安装版
![[probability theory] conditional probability, Bayesian formula, correlation coefficient, central limit theorem, parameter estimation, hypothesis test](/img/2f/f44381ea759f4c1c957a8f9434f0ee.png)
[probability theory] conditional probability, Bayesian formula, correlation coefficient, central limit theorem, parameter estimation, hypothesis test

Scala-day03- operators and loop control
随机推荐
Installing MySQL under Linux (RPM package installation)
PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)
Report on in-depth analysis and investment strategy recommendations for China's petroleum coke industry (2022 Edition)
PHP laravel+gatewayworker completes im instant messaging and file transfer (Chapter 1: basic configuration)
Scala-day03- operators and loop control
NoSQL mongodb - 04 mongodb database and web service combination case
Comparison of latest mobile phone processors in 2020 (with mobile phone CPU ladder diagram)
China Medical Grade hydrogel market supply and demand research and prospect analysis report 2022 Edition
Vscode solves the problem of Chinese garbled code
洛谷P3426 [POI2005]SZA-Template 题解
TSMC Samsung will mass produce 3nm chips in 2022: will the iPhone be the first?
美学心得(第二百三十八集) 罗国正
Redis learning - 06 drifting bottle case
Redis learning - 04 persistence
Operation analysis and investment prospect research report of China's organic chemical raw material manufacturing industry 2022-2028
Generate JDE dot train
NFS shared storage service installation
不到40行代码手撸一个BlocProvider
不到40行代码手撸一个BlocProvider
Rookie practical UML - activity diagram