惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
月光博客
月光博客
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
小众软件
小众软件
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
Vercel News
Vercel News
量子位
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
腾讯CDC
有赞技术团队
有赞技术团队

Dr34m's Blog

I Expect You To Die(我希望你死) 1,2,3 完整中文攻略 Linux换国内源 && docker安装 && 换加速镜像 Vue3 + Element-Plus 极简速查 随笔 pyinstaller打包的程序执行报错Failed to extract xxxx decompression resulted in return code -1 taoSync排除项简易教程 apscheduler的cron配置项 如何在绿联NAS中使用TaoSync同步我的文件到各个网盘 GB28181抓包记录 JS计算字节大小,把字节转换为KB/MB/GB/TB等 在Python中使用onvif管理摄像头,包括设备发现,获取RTSP地址,获取设备信息,截图,云台控制与缩放,设置时间 编写bat脚本实现对vue项目构建并压缩 内网穿透工具frp快速使用 CentOS 7.9 安装基础开发环境jdk redis nacos nginx mysql Pyinstaller 逆向 VUE实现复制与粘贴_获取剪切板内容 Electron + xterm.js + node-pty + vue 实现本地终端 child_process exec 中文乱码 Windows&Linux 解决方法 yum 更新 gcc CentOS安装并使用conda 将fluid主题博客的静态资源由第三方改为本地存储 一个上手即用的通用公众号/小程序/h5/app框架 python批量转化doc到docx SpringBoot单元测试注入空指针 TensorFlow 预测出现NaN的一种可能以及解决方法 随笔 python归一化数据 windows下python安装sasl遇到的问题及解决 Python http.server 本地服务支持跨域 TensorFlow入门
将对象List中的某个字段放到新的List中[转载]
Dr3@m · 2022-03-22 · via Dr34m's Blog

目标

将对象List中的某个字段放到新的List中

转载自:list stream:将对象List中的某个字段放到新的List中

实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Test {
public static void main(String[] args) {
List<TAdslot> userList = new ArrayList<>();
TAdslot adslot1 = new TAdslot();
adslot1.setName("One");
adslot1.setNameList(Lists.newArrayList("A","B","C"));
TAdslot adslot2 = new TAdslot();
adslot2.setName("Zero");
adslot2.setNameList(Lists.newArrayList("D","E","F"));
TAdslot adslot3 = new TAdslot();
adslot3.setName("Two");
adslot3.setNameList(Lists.newArrayList("H","I","J"));
TAdslot adslot4 = new TAdslot();
adslot4.setName("Four");
userList.add(adslot1);
userList.add(adslot2);
userList.add(adslot3);
userList.add(adslot4);

List<String> stringList = userList.stream().map(TAdslot::getName).collect(Collectors.toList());
System.out.println(stringList);

List<String> userListStr = userList.stream().map(TAdslot::getNameList).filter(Objects::nonNull).flatMap(List::stream).collect(Collectors.toList());
System.out.println(userListStr);
}
}

@Data
class TAdslot {
public String name;
public List<String> nameList;
}

输出结果:

[One, Zero, Two, Four]
[A, B, C, D, E, F, H, I, J]