I have a list of objects and I want to combine all object and create single object with all details, this is my class
public class Customer {
private String name;
private String address;
private Integer salary;
private Integer yearOfBirth;
private String city;
private String gender;
private Integer age;
}
Let's say I have list like this:
List<Customer> customers = new ArrayList<>();
customers.add(new Customer("ABC", "QWE", null, null, null, null, null));
customers.add(new Customer(null, null, 12345, 1995, null, null, null));
customers.add(new Customer(null, null, null, null, "zxcv", "M", 12));
I want to create single object with all details like:
Customer customer = Customer("ABC", "QWE", 12345, 1995, "zxcv", "M", 12);
How can I achieve this result using java? I can't think any elegant solution!
Just collect the values out of the List.
Customer customer = new Customer(null, null, null, null, null, null, null)
foreach (Customer tmp : customers)
{
customer.name = (tmp.name == null) ? customer.name : tmp.name;
customer.address = (tmp.address == null) ? customer.address : tmp.address;
// ...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.