如何将列表值收集到收集器对象中

问题描述 投票:1回答:1

我正在尝试从列表对象中收集2个数据字段。

我正在使用Employee对象:

public class Employee
{

  private long id;
  private Source source;
  private String name;
  private String gender;

  // getters
   private Builder toBuilder(Builder builder)
    {
        builder.id = this.summaryDataId;
        builder.name = this.name;
        builder.gender = this.gender;
        builder.source = this.source;
        return builder;
    }

将员工数据放入服务类别的列表中

final List<Employee> employeeData = employeeDao.retrieveEmployeeData(emp.getID());

,然后尝试创建带有employeeId和sourceid的列表(例如:1234:3)。为此,我正在尝试使用collectors.toList

List<String> employeeCollector = employeeData.stream()
                .filter(s -> s.getId != null)
                .filter(s -> s.getSource() != null && s.getSource().getId() != null)
                .collect(Collectors.toList());

我如何使用collectors.toLis()获取employeeid:souceid格式>

我正在尝试从列表对象中收集2个数据字段。我正在使用Employee对象:public class Employee {private long id;私人来源;私有字符串名称;私有字符串...

collectors
1个回答
0
投票

您只需要执行中间操作map来提取员工ID和源ID,>

List<String> employeeCollector = employeeData.stream()
            .filter(s -> s.getId != null)
            .filter(s -> s.getSource() != null && s.getSource().getId() != null)
            .map(s-> String.format("%s:%s",s.getId(),s.getSource().getId()))
            .collect(Collectors.toList());
© www.soinside.com 2019 - 2024. All rights reserved.