如何将公司
的值复制到:
List personList
致企业
:
List employeeList
使用 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
的属性名称。
最佳答案
我假设Employee
有一个像Employee(String name, String enterprise)
您可以使用 stream().map()
将 Person
对象转换为 Employee
,然后收集新的 Employee
code> 列表中的实例。
试试这个:
List<Employee> employeeList = personList.stream().map(p -> new Employee(p.getName(), p.getCompany()))
.collect(Collectors.toList());
关于java - 如何将ArrayList的属性复制到具有不同属性名称的其他ArrayList的另一个属性中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62219787/