我有一个pojo课程:
class Employee{
private int empCode;
private String name;
//getters and setters and Constructor
}
我有另一个类,它使用参数构造函数实例化所有Employee Object。并且所有Employee pojo对象都添加在Arraylist中。
class EmployeeData{
Employee employee = new Employee(111,"XXX");
Employee employee1 = new Employee(222, "YYY");
Employee employee2 = new Employee(444, "BBB");
List<Employee> listData = new ArrayList<Employee>();
listData.add(employee);
listData.add(employee1);
listData.add(employee2);
public List<Employee> getList(List<Employee> list){
return list;
}
}
最后我有一个Main类,它有另一个列表和一个合并方法。
class EmployeeTest{
Employee employee = new Employee(111,"NNN");
Employee employee1 = new Employee(222, "YYY");
Employee employee2 = new Employee(333, "KKK");
List<Employee> list = new ArrayList<Employee>();
list.add(employee);
list.add(employee1);
list.add(employee2);
public List<Employee> merge(List<Employee> list){
//code goes here
return listData;
}
我需要比较列表中的id,如果id匹配但名称不同,我将用列表的值覆盖listData的值。
如果id不匹配,并且在listData中不存在333。所以我们将添加id和值,最后
我试图做这样的事情,但我没有得到正确的输出。请帮忙。
public List<Employee> merge(List<Employee> list){
if(list==null){
return null;
}
List<Employee> list1 = employeeDao.getList();
for(int i =0;i<list1.size();i++){
for(int j=0; j<list1.size();j++){
if(list.get(i).getEmpCode().equals(list2.get(j).getEmpCode())){
list1.set(j, list.get(i));
j++;
}else{
j++;
}
}
}
return list;
}
我无法考虑最后两个逻辑。请帮忙
答案 0 :(得分:0)
检查您是否得到正确答案。这不是好方法,但仍然。
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
class Employee {
private int empCode;
private String name;
public Employee(int empCode, String name) {
super();
this.empCode = empCode;
this.name = name;
}
public int getEmpCode() {
return empCode;
}
public void setEmpCode(int empCode) {
this.empCode = empCode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Employee [empCode=" + empCode + ", name=" + name + "]";
}
}
public class Practice {
public static void main(String[] args) {
Employee employee = new Employee(111,"XXX");
Employee employee1 = new Employee(222, "YYY");
Employee employee2 = new Employee(444, "BBB");
Employee employee3 = new Employee(111,"NNN");
Employee employee4 = new Employee(222, "YYY");
Employee employee5 = new Employee(333, "KKK");
List<Employee> employeeDataList = new ArrayList<Employee>();
employeeDataList.add(employee);
employeeDataList.add(employee1);
employeeDataList.add(employee2);
List<Employee> employeeTestList = new ArrayList<Employee>();
employeeTestList.add(employee3);
employeeTestList.add(employee4);
employeeTestList.add(employee5);
merge(employeeDataList, employeeTestList);
}
public static List<Employee> merge(List<Employee> employeeDataList, List<Employee> employeeTestList) {
List<Employee> list = new ArrayList<Employee>();
for(Employee emp : employeeTestList) {
int lengthOfEmployeeDataList = employeeDataList.size();
int count = 0;
for(Employee empData : employeeDataList){
if(emp.getEmpCode() == empData.getEmpCode()) {
//EMPLOYEE CODE MATCHES
if(!emp.getName().equals(empData.getName())) {
//WHEN NAME ARE NOT EQUAL
empData.setName(emp.getName()); //NAME IN TEST LIST IS ASSIGNED TO DATALIST
list.add(empData);
break;
}
else {
list.add(emp);
break;
}
}
else if(!(emp.getEmpCode() == empData.getEmpCode()) && count < lengthOfEmployeeDataList){
//ID DOESNT MATCHES
count++;
}
if(count == lengthOfEmployeeDataList){
//ALL THE EMPLOYEE Objects has been Checked and None Matching
list.add(emp);
}
}
}
for(Employee emp : employeeDataList) {
if(!list.contains(emp)) {
employeeDataList.remove(emp);
}
}
System.out.println(list);
return list;
}
}
答案 1 :(得分:0)
如果List不是唯一的解决方案,我建议使用Class HashMap并将id用作键。然后你可以使用HashMap api,这对这些动作更有帮助,例如的putIfAbsent() https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html