当前位置:网站首页>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()方法,将每行内容转化成流
边栏推荐
- Is it safe to open a securities account in general
- Oracle锁表查询和解锁方法
- 程序员必备,一款让你提高工作效率N倍的神器uTools
- Laravel subdomain accesses different routing files and different modules
- 小程序中控件里面的内容较多,让其支持滚动的良好方案
- 7-1 数的范围
- VMware virtual machine bridging mode can not access the campus network "suggestions collection"
- nvm安装教程
- What are the top ten securities companies? Is it safe to open a mobile account?
- 一个初级多线程服务器模型
猜你喜欢

NoSQL mongodb - 04 mongodb database and web service combination case

Fengshentai old shooting range Kali series

Implementing mixins scheme in applet

Laravel subdomain accesses different routing files and different modules

NoSQL mongodb - 01 introduction to NoSQL and mongodb

小程序中控件里面的内容较多,让其支持滚动的良好方案

Research and development practice of Kwai real-time data warehouse support system

This executeQuery (SQL) cannot compile classes for JSP. What is the reason?

简易数字电路交通灯设计

快手实时数仓保障体系研发实践
随机推荐
dried food! Yiwen will show you SD card, TF card and SIM card!
[solved] data duplication or data loss after laravel paginate() paging
fastjson的JSONArray和JSONObject[通俗易懂]
一个初级多线程服务器模型
详细实操分享,下班刷了两小时的搞笑视频,一个月收益7000多
不到40行代码手撸一个BlocProvider
Fengshentai old shooting range Kali series
Laravel subdomain accesses different routing files and different modules
What software is flush? Is online account opening safe?
PHP generate order number
PHP laravel+gatewayworker completes im instant messaging and file transfer (Chapter 1: basic configuration)
小程序中控件里面的内容较多,让其支持滚动的良好方案
Research and development practice of Kwai real-time data warehouse support system
Example of parameter passing from laravel query constructor to closure method
Oracle锁表查询和解锁方法
一个快速切换一个底层实现的思路分享
初探Protostuff的使用[通俗易懂]
Php+laravel5.7 use Alibaba oss+ Alibaba media to process and upload image / video files
New routing file in laravel framework
Polarismesh series articles - concept series (I)