我有一个EmployeeDetails对象的arrayList和另一个EmployeeSalary对象的arrayList。我想创建一个同时具有EmployeeDetails和EmployeeSalary属性的新ArrayList.My EmployeeDetails类具有属性" id"和"名称"。我的EmployeeSalary类有属性" id"和#34;薪水"。我想要一个带有属性" id"," name"的arraylist和"薪水"。
EmployeeDetails Class
public class EmployeeDetails {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
EmployeeSalary类
public class EmployeeSalary {
private String id;
private String sal;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSal() {
return sal;
}
public void setSal(String sal) {
this.sal = sal;
}
}
答案 0 :(得分:1)
EmployeeSalary应该扩展Employeedetails
public class EmployeeSalary extends EmployeeDetails{
private String sal;
...
之后,为了合并两个列表,应该调用arraylist1.addAll(arraylist2)。
答案 1 :(得分:1)
首先,设置一张地图,以便从ID中访问工资:
Map<String, String> salaryMappings = new HashMap();
for(EmployeeSalary salary : salaries) {
salaryMappings.put(salary.getId(), salary.getSal());
}
(您的工资ArrayList名为salaries
)。
接下来,你需要一个类来存储信息(注意我不是为了保持帖子的简短而不是编写访问者,尽管你应该添加它们):
class EmployeeInformation {
String id;
String name;
String sal;
public EmployeeInformation(String id, String name, String sal) {
this.id = id;
this.name = name;
this.sal = sal;
}
}
最后,将值复制到新的ArrayList中:
List<EmployeeInformation> infos = new ArrayList<EmployeeInformation>();
for(EmployeeDetails detail : details) {
infos.add(new EmployeeInformation( details.getId(), details.getName(), salaryMappings.get(details.getId()) ));
}
答案 2 :(得分:1)
无需使用HashMap或Map。只需使用java 8 lambda :)
创建EmployeeInformation.java类。
public class EmployeeInformation {
String id;
String name;
String sal;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSal() {
return sal;
}
public void setSal(String sal) {
this.sal = sal;
}
public EmployeeInformation(String id, String name, String sal) {
this.id = id;
this.name = name;
this.sal = sal;
}
}
找到完整的解决方案。
List<EmployeeDetails> employeeDetailsList = new ArrayList<>();
EmployeeDetails employeeDetails1 = new EmployeeDetails();
employeeDetails1.setId("A");
employeeDetails1.setName("EMP1");
employeeDetailsList.add(employeeDetails1);
EmployeeDetails employeeDetails2 = new EmployeeDetails();
employeeDetails2.setId("B");
employeeDetails2.setName("EMP2");
employeeDetailsList.add(employeeDetails2);
List<EmployeeSalary> employeeSalariesList = new ArrayList<>();
EmployeeSalary employeeSalary1 = new EmployeeSalary();
employeeSalary1.setId("A");
employeeSalary1.setSal("SAL1");
employeeSalariesList.add(employeeSalary1);
EmployeeSalary employeeSalary2 = new EmployeeSalary();
employeeSalary2.setId("B");
employeeSalary2.setSal("SAL2");
employeeSalariesList.add(employeeSalary2);
List<EmployeeInformation> employeeInformationList = new ArrayList<>();
employeeDetailsList.forEach(employeeDetails -> {
String _id = employeeDetails.getId();
String _name = employeeDetails.getName();
employeeSalariesList.stream().filter(employeeSalary -> employeeSalary.getId().equalsIgnoreCase(_id)).forEach(employeeSalary -> {
EmployeeInformation employeeInformation = new EmployeeInformation(_id, _name, employeeSalary.getSal());
employeeInformationList.add(employeeInformation);
});
});
for (EmployeeInformation employeeInformation : employeeInformationList) {
System.out.println(employeeInformation.getId() + "-" + employeeInformation.getName() + "-" + employeeInformation.getSal());
}
输出 - &gt;
A-EMP1-SAL1
B-EMP2-SAL2
答案 3 :(得分:0)
您可以将这两个集合转换为id
- &gt; value
的地图,然后进行辅助查找。
以下构建Map<String, String>
的集合,其中包含结果中所需的键:
List<EmployeeDetails> details = ...;
List<EmployeeSalary> salaries = ...;
Map<String, EmployeeDetails> detailsMap = details.stream()
.collect(Collectors.groupingBy(EmployeeDetails::getId,
Collectors.reducing(null, (e1, e2) -> e1)));
Map<String, EmployeeSalary> salariesMap = salaries.stream()
.collect(Collectors.groupingBy(EmployeeSalary::getId,
Collectors.reducing(null, (e1, e2) -> e1)));
List<Map<String, String>> summaries = detailsMap.entrySet().stream()
.map(entry -> {
Map<String, String> m = new HashMap<>();
m.put("id", entry.getKey());
m.put("name", entry.getValue().getName());
m.put("salary", salariesMap.get(entry.getKey()).getSal());
return m;
}).collect(Collectors.toList());
您可能会选择为摘要构建单独的类,包含id
,name
和salary
字段,而不是使用一个Map
。但这是一个如何去做的想法。
答案 4 :(得分:0)
我认为,最好的解决方案是创建另一个类(例如EmployeeData
)并在那里收集所有必需的数据。如果您不希望(或不能)修改EmployeeDetails
和EmployeeSalry
的定义。
class EmployeeData {
private final String id;
private String name;
private String sal;
public EmployeeData(String id) {
this.id = id;
}
}
private static List<EmployeeData> merge(List<EmployeeDetails> details, List<EmployeeSalary> salaries) {
Map<String, EmployeeData> data = new HashMap<>();
String id;
for (EmployeeDetails detail : details) {
id = detail.getId();
if (!data.containsKey(id))
data.put(id, new EmployeeData(id));
data.get(id).setName(detail.getName());
}
for (EmployeeSalary salary : salaries) {
id = salary.getId();
if (!data.containsKey(id))
data.put(id, new EmployeeData(id));
data.get(id).setSal(salary.getSal());
}
return new ArrayList<>(data.values());
}