Java 如何在一个属性不同的两个数组列表中查找普通员工?
我有两个数组列表。每个都有Employee类型的对象列表 Employee类如下所示Java 如何在一个属性不同的两个数组列表中查找普通员工?,java,arraylist,Java,Arraylist,我有两个数组列表。每个都有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 assign
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.
}
}
请帮忙
尽管在SQL中很容易做到这一点,但我不能在SQL查询中做到这一点,因为我不能修改查询。我只需要使用两个给定的列表:(将equals()
、hashcode()
和compareTo()
方法添加到员工
类中。然后您可以尝试设置操作,例如保留()
或从集合静态类中删除所有()
如果要在任何时候以任何方式对这些方法进行比较,最好将它们添加到类中。重写hashcode和equals()方法重写下面的equals
方法
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (employeeId == null) {
if (other.employeeId != null)
return false;
} else if (!employeeId.equals(other.employeeId))
return false;
if (firstname == null) {
if (other.firstname != null)
return false;
} else if (!firstname.equals(other.firstname))
return false;
if (Float.floatToIntBits(fte) == Float.floatToIntBits(other.fte))
return false;
if (lastname == null) {
if (other.lastname != null)
return false;
} else if (!lastname.equals(other.lastname))
return false;
return true;
}
然后创建一个finalList
列表,并将list1
添加到列表中。然后使用list2
调用finalList
列表中的retainal,该列表将根据employeeId
List<Employee> finalList=new ArrayList<Employee>();
finalList.addAll(list1);
finalList.retainAll(list2);
List<Employee> commonInBothListWithDifferentFTE=finalList;
System.out.println(commonInBothListWithDifferentFTE);
如果员工的最大数量是500人甚至1000人左右,只需使用双嵌套循环。它应该足够快。此方法的时间复杂度为O(mn),其中m和n是两个列表的大小
如果你期望员工的最大数量是3000甚至更多,你可以考虑这2种方法:
另一种方法是根据员工编号的字典顺序对两个列表进行排序(实现一个比较器
),并使用单个循环同时循环两个列表。复杂性为O(mlog m+nlog n)
如果您想更优化,可以使用HashSet(覆盖equals
和hashCode
)并将一个列表的所有元素放入HashSet。您现在可以循环浏览另一个列表,从第一个列表中选择同一个员工并进行比较。分摊的复杂度为O(m+n).只需使用双嵌套循环进行比较和查找。在上实现hashcode和equalsemployees@nhahtdh.但是列表将是巨大的。大约500名员工。时间复杂性的可能重复并不比双重嵌套循环好。它现在隐藏在List
的retainal
方法中。
List<Employee> finalList=new ArrayList<Employee>();
finalList.addAll(list1);
finalList.retainAll(list2);
List<Employee> commonInBothListWithDifferentFTE=finalList;
System.out.println(commonInBothListWithDifferentFTE);
[Employee [firstname=F1, lastname=L1, employeeId=EMP01, fte=1.0], Employee [firstname=F9, lastname=L9, employeeId=EMP09, fte=0.7]]