使用Lambda表达式给对象赋值
2021/4/16 18:55:21
本文主要是介绍使用Lambda表达式给对象赋值,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
使用Lambda表达式给对象赋值
- 简化代码
代码演示
- 如下
import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; public class Test { public static void main(String[] args) { InfoNoticeItem re2 = new InfoNoticeItem(); re2.setIds("2"); re2.setStoreCode("高佳琪"); InfoNoticeItem re4 = new InfoNoticeItem(); re4.setIds("4"); re4.setStoreCode("王莹莹"); List<InfoNoticeItem> list = new ArrayList<>(); Collections.addAll(list, re2, re4); ShopInfoNoticeReq shopInfoNoticeReq = new ShopInfoNoticeReq(); shopInfoNoticeReq.setFormat(null); shopInfoNoticeReq.setIdList(list); // 赋值.setIdList(collect) // public void setIdList(List<InfoNoticeItem> idList) List<InfoNoticeItem> collect = shopInfoNoticeReq.getIdList().stream().map(item -> { InfoNoticeItem ini = new InfoNoticeItem(); ini.setIds(item.getIds()); ini.setStoreCode(item.getStoreCode()); return ini; }).collect(Collectors.toList()); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(JSON.toJSON(collect)); } }
输出
- 原本格式: [InfoNoticeItem(ids=2, storeCode=高佳琪), InfoNoticeItem(ids=4, storeCode=王莹莹)]
- JSON格式后: [{"ids":"2","storeCode":"高佳琪"},{"ids":"4","storeCode":"王莹莹"}]
这篇关于使用Lambda表达式给对象赋值的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)