[英]How to combine two List<Object[]> objects into a single List<Object[]>
[英]How to combine list of objects to single object java?
我有一個對象列表,我想組合所有 object 並創建一個包含所有詳細信息的 object,這是我的 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;
}
假設我有這樣的列表:
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));
我想創建單個 object 的所有詳細信息,例如:
Customer customer = Customer("ABC", "QWE", 12345, 1995, "zxcv", "M", 12);
如何使用 java 實現此結果? 我想不出任何優雅的解決方案!
只需從列表中收集值。
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;
// ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.