gpt4 book ai didi

java - 如何在一个属性不同的两个数组列表中找到普通员工?

转载 作者:行者123 更新时间:2023-11-30 11:42:44 25 4
gpt4 key购买 nike

我有两个数组列表。每个都有 Employee 类型的对象列表。

Employee 类如下所示

    public class Employee {

private int id; // this is the primary key from employee table

private String firstname;

private String lastname;

private String employeeId; // manually assigned unique id to each employee

private float fte;

Employee(String firstname, String lastname, String employeeId, float fte) {
this.firstname = firstname;
this.lastname = lastname;
this.employeeId = employeeId;
this.fte = fte;
}

// getters and setters
}

员工 ID 是手动生成的唯一 ID,分配给每位员工。

我需要根据具有不同 fte 的员工 ID 在两个列表之间找到共同的员工。

    import java.util.ArrayList;
import java.util.List;

public class FindFTEDifferencesBetweenMatchingEmployeeIds {

public static void main(String args[]) {
List<Employee> list1 = new ArrayList<Employee>();
List<Employee> list2 = new ArrayList<Employee>();

list1.add(new Employee("F1", "L1", "EMP01", 1));
list1.add(new Employee("F2", "L2", "EMP02", 1));
list1.add(new Employee("F3", "L3", "EMP03", 1));
list1.add(new Employee("F4", "L4", "EMP04", 1));
list1.add(new Employee("F5", "L5", "EMP05", 1));
list1.add(new Employee("F9", "L9", "EMP09", 0.7F));

list2.add(new Employee("F1", "L1", "EMP01", 0.8F));
list2.add(new Employee("F2", "L2", "EMP02", 1));
list2.add(new Employee("F6", "L6", "EMP06", 1));
list2.add(new Employee("F7", "L7", "EMP07", 1));
list2.add(new Employee("F8", "L8", "EMP08", 1));
list2.add(new Employee("F9", "L9", "EMP09", 1));

List<FTEDifferences> commonInBothListWithDifferentFTE = new ArrayList<FTEDifferences>();
// this should contain EMP01 and EMP09
// since EMP02 has same FTE in both lists, it is ignored.


}
}

员工 ID 为 EMP01 和 EMP09 的员工在两个列表中都很常见,而且他们在每个列表中的全职也不同。

所以,我想要另一个包含这两名员工的列表。

    public class FTEDifferences {

private Employee fromList1;

private Employee fromList2;

public Employee getFromList1() {
return fromList1;
}

public void setFromList1(Employee fromList1) {
this.fromList1 = fromList1;
}

public Employee getFromList2() {
return fromList2;
}

public void setFromList2(Employee fromList2) {
this.fromList2 = fromList2;
}
}

请帮忙。

附言。尽管在 SQL 中很容易做到这一点,但我不能在 SQL 查询中做到这一点,因为我无法修改查询。我只需要处理两个给定的列表。 :(

最佳答案

equals()hashcode()compareTo() 方法添加到您的 Employee 类。然后您可以尝试设置操作,例如 Collections 静态类中的 retainAll()removeAll()

如果要在任何时候以任何方式比较这些方法,最好将这些方法添加到类中。

关于java - 如何在一个属性不同的两个数组列表中找到普通员工?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11555362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com