java - 有没有从对象列表中获取字符串列表的有效方法?
问题描述
是否有从包含字符串字段的列表中获取字符串列表的有效方法。
即我有客户对象和约会对象
public Customer {
String customerId;
String name;
List<Appointment> appointments;
public String getCustomerId() {return customerId;}
public String getName() {return name;}
public List<Appointment> getAppointments() {return appointments;}
}
public Appointments {
String appointmentId;
String employee;
}
现在作为客户,我可以有几个不同的约会。如果我只想获取与客户关联的所有约会 ID 的列表怎么办?
类似 -> customer.getAppointments().getId;?
解决方案
通过使用流 api:
List<String> idList = someCustomer.getAppointments()
.stream()
.map(Appointment::getId)
.collect(Collectors.toList());
推荐阅读
- android - 有没有一种方法可以使用 MLKit 检测皮肤上的损伤
- c# - 将技能中的信息存储/发送到 Microsoft 虚拟助手
- javascript - 使用 Firebase 时未设置 DisplayName
- python - speedtest-cli python脚本,插入sql值错误
- java - Java 9 takeWhile 和 dropWhile 读取和跳过某些行
- flutter - 如何在 Flutter 中更改复制/粘贴工具栏的位置
- python-3.x - 如何使用flask检索存储在python中每个用户的会话存储中的相应数据集?
- python - 引用无效字符号:(Python ElementTree 解析)
- javascript - 如何将新的承诺对象从后端服务文件返回到后端控制器?
- c++ - 如何将文件中的数据读取到类的数组中