当前位置:网站首页>Simple use of stream (II)
Simple use of stream (II)
2022-06-25 11:41:00 【Duxiaolie】
List of articles
Preface
demand : I need to give List Properties of objects in phone Assign a value , And then back to the front end . Current properties phone The value of is in another table .
Tips : The following is the main body of this article , The following cases can be used for reference
One 、 Use steps
1. Sample code
List<MecReceiveLine> mecReceiveLines = mapper.selectByDepartId(departId);
// Convert to VO
List<MecReceiveLineVO> mecReceiveLineVOS = BeanHelper.copyWithCollection(mecReceiveLines, MecReceiveLineVO.class);
List<String> departIds = mecReceiveLines.stream().map(mecReceiveLine -> mecReceiveLine.getDepartId())
.collect(Collectors.toList());
List<MecDepartInfo> mecDepartInfoList = mecDepartInfoBiz.selectByIds(departIds);
// Collect to List,
// take List Object to map;(Function.identity()) Use objects as value.
/** * Take the primary key of the object id, effect key, Ensure accurate correspondence . */
Map<String, MecDepartInfo> collect = mecDepartInfoList.stream().collect(Collectors.toMap(MecDepartInfo::getCustPk, Function.identity()));
// The goal is : loop VOS object , For its phone Property to assign . Processing must be the target object , It's assigned , It is also where the demand lies .
for (MecReceiveLineVO mecReceiveLineVO : mecReceiveLineVOS) {
MecDepartInfo mecDepartInfo = collect.get(mecReceiveLineVO.getDepartId());
if (mecDepartInfo != null){
mecReceiveLineVO.setPhoneNumber(mecDepartInfo.getCustMobile());
}
}
2. Interface profile (SQL)
<!-- Batch query -->
<select id="selectByDepartIds" resultType="com.guanke.dmp.mec.entity.MecDepartInfo">
select
*
from mec_depart_info
where ( 1 = 1 )
<if test="departIds != null and departIds.size != 0">
and cust_pk in
<foreach collection="departIds" item="item" open=" ( " close=" ) " separator=",">
#{item}
</foreach>
</if>
</select>
Here we use Subquery and dynamic sql.
3.Bean Tool class
/** * @author Yingyu Wei * @date 2020-03-18 */
@Slf4j
public class BeanHelper {
public static <T> T copyProperties(Object source, Class<T> target){
try {
T t = target.newInstance();
BeanUtils.copyProperties(source, t);
return t;
} catch (Exception e) {
log.error("【 Data conversion 】 Data conversion error , Target audience {} Constructor exception ", target.getName(), e);
throw new RuntimeException("【 Data conversion 】 Data conversion error ");
}
}
public static <T> List<T> copyWithCollection(List<?> sourceList, Class<T> target){
try {
return sourceList.stream().map(s -> copyProperties(s, target)).collect(Collectors.toList());
} catch (Exception e) {
log.error("【 Data conversion 】 Data conversion error , Target audience {} Constructor exception ", target.getName(), e);
throw new RuntimeException("【 Data conversion 】 Data conversion error ");
}
}
public static <T> Set<T> copyWithCollection(Set<?> sourceList, Class<T> target){
try {
return sourceList.stream().map(s -> copyProperties(s, target)).collect(Collectors.toSet());
} catch (Exception e) {
log.error("【 Data conversion 】 Data conversion error , Target audience {} Constructor exception ", target.getName(), e);
throw new RuntimeException("【 Data conversion 】 Data conversion error ");
}
}
}
Add
Example 2
demand : Need to be right List The attribute assignment of the object in the .
Use :forEach() Method , Instead of using streams .
// skuList.stream().map(sku1 -> sku1.setSpuId(spuDTO.getId())); error
skuList.forEach(sku1 -> sku1.setSpuId(spuDTO.getId()));
summary
Summary of notes :
1, to List When assigning a property in , Need to traverse , Just use stream Flow through row operation .
1.1, Sample code 1 The attributes of objects in requirements are variable ; Sample code 2 The attribute values of objects in requirements are fixed , So use forEach() Faster .
2,list Convert to map when , Streams can also be used , And use list Object id( It's usually used id, Because it doesn't repeat , accord with key The requirements of .) As key, Using objects as Value.
// @AllArgsConstructor
Recommended articles :link.s
2021 year 3 month 25 Japan
Add : The value of the object property does not change , It can be used forEach().(ps: This is not a stream operation )
边栏推荐
- A program reflecting the characteristics of C language program structure
- Keywords serializable serialization and deserialization
- Double tampon transparent cryptage et décryptage basé sur le cadre minifilter
- 兴业证券是国企吗?在兴业证券开户资金安全吗?
- Subclass a inherits from parent class B, a a = new a(); Then the execution sequence of the constructor of parent class B, the static code block of parent class B, the non static code block of parent c
- Two ways of redis persistence -- detailed explanation of RDB and AOF
- What are the ways to simulate and burn programs? (including common tools and usage)
- wait()、notify()和notifyAll()、sleep()、Condition、await()、signal()
- Kingbasees plug-in ftutilx of Jincang database
- 4 life distributions
猜你喜欢

Idea local launch Flink task

记一次给OpenHarmony提交代码的过程

Flink deeply understands the graph generation process (source code interpretation)

Spark history server performance improvement (I) -- Application List

Démarrer avec Apache shenyu

翌圣生物冲刺科创板:25%收入来自新冠产品销售 拟募资11亿

芯片的发展史和具体用途以及结构是什么样的

4 life distributions

Crawler scheduling framework of scratch+scratch+grammar

SQL注入漏洞(繞過篇)
随机推荐
Keywords serializable serialization and deserialization
C disk uses 100% cleaning method
Recommend a virtual machine software available for M1 computer
Free access to the global human settlements layer (ghsl) dataset from Gee
2022 mathematical modeling competition time and registration fee
GC
TCP如何处理三次握手和四次挥手期间的异常
SQL注入漏洞(绕过篇)
Source code analysis of AQS & reentrantlock
Wait (), notify (), notifyAll (), sleep (), condition, await (), signal()
Double buffer transparent encryption and decryption driven course paper + project source code based on minifilter framework
Countdownlatch source code analysis
A program reflecting the characteristics of C language program structure
手机上股票开户安全吗?找谁可以开户啊?
数据库系列:MySQL索引优化总结(综合版)
Jincang KFS data centralized scenario (many to one) deployment
JVM 原理简介
基于超算平台气象预警并行计算架构研究
Use of Presto visualization client-yanagishima20.0
CMU提出NLP新范式—重构预训练,高考英语交出134高分