How copy property of ArrayList in another property of other ArrayList with different property name?

huangapple go评论80阅读模式
英文:

How copy property of ArrayList in another property of other ArrayList with different property name?

问题

personList中的company的值复制到以下的employeeList中的enterprise,使用Java-8怎么做?

我有:

@Getter
@Setter
public class Person {
    private String name;
    private String company;
}

和:

@Getter
@Setter
public class Employee {
    private String name;
    private String enterprise;
}
List<Person> personList;
List<Employee> employeeList;

我不能更改enterprise属性的名称。

英文:

How copy values of company in:

>List<Person> personList

To enterprise of:

>List<Employee> employeeList

With Java-8?

I have:

@Getter
@Setter
public class Person {
    private String name;
    private String company;

}

And:

@Getter
@Setter
public class Employee {
    private String name;
    private String enterprise;

}
List&lt;Person&gt; personList
List&lt;Employee&gt; employeeList

I can't change the property name of enterprise.

答案1

得分: 1

假设 Employee 有一个类似于 Employee(String name, String enterprise) 的构造函数。

您可以通过使用 stream().map()Person 对象转换为 Employee,然后在一个列表中收集新的 Employee 实例。

试一试:

List<Employee> employeeList = personList.stream().map(p -> new Employee(p.getName(), p.getCompany()))
        .collect(Collectors.toList());
英文:

I assume Employee has a constructor like Employee(String name, String enterprise)

You can convert Person objects to Employee by using stream().map(), then collect new Employee instances in a list.

Try this:

List&lt;Employee&gt; employeeList = personList.stream().map(p -&gt; new Employee(p.getName(), p.getCompany()))
		.collect(Collectors.toList());

答案2

得分: 0

你可以使用 Lombok 框架的其他方法,比如 Builder

另外,你可以创建一个用于将 Person 转换为 Employee 的实用工具类。

而使用 Stream API 的实现将会是:

List<Person> personList = List.of(Person.builder()
                    .name("name1")
                    .company("company1")
                    .build(),
            Person.builder()
                    .name("name2")
                    .company("company2")
                    .build());

System.out.println(personList);

List<Employee> employeeList = personList.stream()
            .map(EmployeeConverter::convertToEmployee)
            .collect(Collectors.toList());

System.out.println(employeeList);

输出:

>[Person(name=name1, company=company1), Person(name=name2, company=company2)]
>[Employee(name=name1, enterprise=company1), Employee(name=name2, enterprise=company2)]

其他代码细节:

@UtilityClass
class EmployeeConverter {
    public Employee convertToEmployee(Person person) {
        return Employee.builder()
                .name(person.getName())
                .enterprise(person.getCompany())
                .build();
    }
}

@Getter
@Setter
@Builder
@ToString
class Person {
    private String name;
    private String company;
}

@Getter
@Setter
@Builder
@ToString
class Employee {
    private String name;
    private String enterprise;
}
英文:

You could use some other methods from Lombok framework, like Builder.

Also, you could create a utility class for converting Person to Employee.

And your implementation with Stream API will be:

List&lt;Person&gt; personList = List.of(Person.builder()
                .name(&quot;name1&quot;)
                .company(&quot;company1&quot;)
                .build(),
        Person.builder()
                .name(&quot;name2&quot;)
                .company(&quot;company2&quot;)
                .build());

System.out.println(personList);

List&lt;Employee&gt; employeeList = personList.stream()
        .map(EmployeeConverter::convertToEmployee)
        .collect(Collectors.toList());

System.out.println(employeeList);

Output:

>[Person(name=name1, company=company1), Person(name=name2, company=company2)]
>[Employee(name=name1, enterprise=company1), Employee(name=name2, enterprise=company2)]

Other code details:

@UtilityClass
class EmployeeConverter {
    public Employee convertToEmployee(Person person) {
        return Employee.builder()
                .name(person.getName())
                .enterprise(person.getCompany())
                .build();
    }
}

@Getter
@Setter
@Builder
@ToString
class Person {
    private String name;
    private String company;
}

@Getter
@Setter
@Builder
@ToString
class Employee {
    private String name;
    private String enterprise;
}

huangapple
  • 本文由 发表于 2020年6月6日 00:15:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/62219787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定