Create a list of data carriers with java Streams - Stack Overflow

I want to convert a list of nested objects into a flat list using Java streams.For this reason, a list

I want to convert a list of nested objects into a flat list using Java streams. For this reason, a list of companies is first drawn up with employees:

 // create Apple Employees
        List<Employee> appleEmployees = List.of(
                new Employee("John", "Doe"), new Employee("Joe", "Bloggs")
        );
        Company apple = new Company("Apple", appleEmployees);


        // create Microsoft Employees
        List<Employee> microsoftEmployees = List.of(
                new Employee("John", "Public"), new Employee("Harry", "Fortune")
        );
        Company microsoft = new Company("Microsofr", microsoftEmployees);


        // put apple and microsft in a list
        List<Company> companies = List.of(apple, microsoft);

        // data carrier
        record CompanyData(String companyName,String employeeName, String employeeSurname) {}

What do I have to do now if I use Java Streams:

// How to stream this?
        List<CompanyData> companyData = companies.stream().????

We should have a list of 4 CompanyData objects at the end of the stream, which contains the name of the company and the first and last name of the employee, something like this:

  List<CompanyData> companyData = List.of(
            new CompanyData("Apple", "John", "Doe"),
            new CompanyData("Apple", "Joe", "Bloggs"),
            new CompanyData("Microsoft", "John", "Public"),
            new CompanyData("Microsoft", "Harry", "Fortune")
    );

Is this even possible and should Java streams be used for the case?

I want to convert a list of nested objects into a flat list using Java streams. For this reason, a list of companies is first drawn up with employees:

 // create Apple Employees
        List<Employee> appleEmployees = List.of(
                new Employee("John", "Doe"), new Employee("Joe", "Bloggs")
        );
        Company apple = new Company("Apple", appleEmployees);


        // create Microsoft Employees
        List<Employee> microsoftEmployees = List.of(
                new Employee("John", "Public"), new Employee("Harry", "Fortune")
        );
        Company microsoft = new Company("Microsofr", microsoftEmployees);


        // put apple and microsft in a list
        List<Company> companies = List.of(apple, microsoft);

        // data carrier
        record CompanyData(String companyName,String employeeName, String employeeSurname) {}

What do I have to do now if I use Java Streams:

// How to stream this?
        List<CompanyData> companyData = companies.stream().????

We should have a list of 4 CompanyData objects at the end of the stream, which contains the name of the company and the first and last name of the employee, something like this:

  List<CompanyData> companyData = List.of(
            new CompanyData("Apple", "John", "Doe"),
            new CompanyData("Apple", "Joe", "Bloggs"),
            new CompanyData("Microsoft", "John", "Public"),
            new CompanyData("Microsoft", "Harry", "Fortune")
    );

Is this even possible and should Java streams be used for the case?

Share Improve this question asked Mar 23 at 0:33 Hakan KiyarHakan Kiyar 435 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

You can use flatMap:

List<CompanyData> companyData = companies.stream().flatMap(company ->
    company.getEmployees().stream().map(employee -> 
        new CompanyData(company.getName(), employee.getName(), employee.getSurname())
    )).toList();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744299971a4567451.html

相关推荐

  • Create a list of data carriers with java Streams - Stack Overflow

    I want to convert a list of nested objects into a flat list using Java streams.For this reason, a list

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信