学习目标:
Java 中stream().map().collect()用法
Java 入门知识
学习内容:
Java 中stream().map().collect()用法
参考原文档注释
- Returns a possibly parallel Stream with this collection as its source. It is allowable for this method to return a sequential stream.
- This method should be overridden when the spliterator() method cannot return a spliterator that is IMMUTABLE, CONCURRENT, or late-binding.
- Returns: a possibly parallel Stream over the elements in this collection
- Implementation Requirements:The default implementation creates a parallel Stream from the collection’s Spliterator.
应用实例:
通俗来讲就是从原有的集合或者数组中按照既定的规则筛选出一组新的数列或者集合
//数据库返回的数据
List<ActivityKillGoods> existList = baseMapper.findAll(null, store, ascId, type);
List<ActivityKillGoodsBean> existBeanList = existList.stream().map(killGoods ->
//添加操作
{
ActivityKillGoodsBean killGoodsBean = new ActivityKillGoodsBean();
//更改值或者设置条件
killGoodsBean.setGoods(killGoods.getGoods());
//返回结果
return killGoodsBean;
}).collect(Collectors.toList());
对于开发数据库数据重复编辑时相比较for更加方便,简单代码量少
总结:
提示:这里统计学习计划的总量
stream有点:
1、无存储。stream不是一种数据结构,它只是某种数据源的一个视图,数据源可以是一个数组,Java容器或I/O channel等。
2、为函数式编程而生。对stream的任何修改都不会修改背后的数据源,比如对stream执行过滤操作并不会删除被过滤的元素,而是会产生一个不包含被过滤元素的新stream。
3、 惰式执行。stream上的操作并不会立即执行,只有等到用户真正需要结果的时候才会执行。
4、可消费性。stream只能被“消费”一次,一旦遍历过就会失效,就像容器的迭代器那样,想要再次遍历必须重新生成