英文:
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<Person> personList
List<Employee> 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<Employee> employeeList = personList.stream().map(p -> 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<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);
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论