Tôi có hai danh sách mảng. Mỗi danh sách có các đối tượng kiểu nhân viên.Làm thế nào để tìm sự khác biệt giữa hai Danh sách Mảng dựa trên một tài sản?
lớp Các nhân viên trông giống như dưới đây
public class Employee {
Employee(String firstname, String lastname, String employeeId) {
this.firstname = firstname;
this.lastname = lastname;
this.employeeId = employeeId;
}
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
// getters and setters
}
Tôi cần phải tìm sự khác biệt giữa hai danh sách dựa trên một thuộc tính của đối tượng nhân viên đó là id của nhân viên.
Id nhân viên là id duy nhất được tạo thủ công cho mỗi nhân viên.
import java.util.ArrayList;
import java.util.List;
public class FindDifferences {
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"));
list1.add(new Employee("F2", "L2", "EMP02"));
list1.add(new Employee("F3", "L3", "EMP03"));
list1.add(new Employee("F4", "L4", "EMP04"));
list1.add(new Employee("F5", "L5", "EMP05"));
list2.add(new Employee("F1", "L1", "EMP01"));
list2.add(new Employee("F2", "L2", "EMP02"));
list2.add(new Employee("F6", "L6", "EMP06"));
list2.add(new Employee("F7", "L7", "EMP07"));
list2.add(new Employee("F8", "L8", "EMP08"));
List<Employee> notPresentInList1 = new ArrayList<Employee>();
// this list should contain EMP06, EMP07 and EMP08
List<Employee> notPresentInList2= new ArrayList<Employee>();
// this list should contain EMP03, EMP04 and EMP05
}
}
Điều gì xảy ra nếu đối tượng của bạn không nhất quán? Ví dụ, nếu list1 chứa ("F1", "L1", "EMPO1") và list2 chứa ("F11", "L11", "EMP01"). Điều đó có được trả lại như không có trong danh sách khác, mặc dù khóa là như nhau? –
@Disco 3. Chúng tôi chỉ tìm kiếm id nhân viên khác nhau. Trong trường hợp của bạn, họ sẽ được xem như nhau. – ashishjmeshram
Lớp nhân viên của bạn sẽ cần phải thực hiện Comparable –