我有两个数组列表。每个都有用户类型的对象列表。如何比较在Java中至少有一个属性不同的类似对象的两个数组列表?
User类看起来像下面
public class User {
private long id;
private String empCode;
private String firstname;
private String lastname;
private String email;
public User(String firstname, String lastname, String empCode, String email) {
super();
this.empCode = empCode;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}
// getters and setters
}
import java.util.ArrayList;
import java.util.List;
public class FindSimilarUsersWithAtLeastOneDifferentProperty {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<User> list1 = new ArrayList<User>();
list1.add(new User("F11", "L1", "EMP01", "[email protected]"));
list1.add(new User("F2", "L2", "EMP02", "[email protected]"));
list1.add(new User("F3", "L3", "EMP03", "[email protected]"));
list1.add(new User("F4", "L4", "EMP04", "[email protected]"));
list1.add(new User("F5", "L5", "EMP05", "[email protected]"));
list1.add(new User("F9", "L9", "EMP09", "[email protected]"));
list1.add(new User("F10", "L10", "EMP10", "[email protected]"));
List<User> list2 = new ArrayList<User>();
list2.add(new User("F1", "L1", "EMP01", "[email protected]"));
list2.add(new User("F2", "L2", "EMP02", "[email protected]"));
list2.add(new User("F6", "L6", "EMP06", "[email protected]"));
list2.add(new User("F7", "L7", "EMP07", "[email protected]"));
list2.add(new User("F8", "L8", "EMP08", "[email protected]"));
list2.add(new User("F9", "L9", "EMP09", "[email protected]"));
list2.add(new User("F100", "L100", "EMP10", "[email protected]"));
List<User> resultList = new ArrayList<User>();
// this list should contain following users
// EMP01 (common in both list but differs in firstname)
// EMP02 (common in both list but differs in email)
// EMP10 (common in both list but differs in firstname, lastname and email)
}
}
如果你看到示例代码,两个列表有四个用户EMP代码EMP01,EMP02,EMP09和EMP10常见。
所以,我们只需要比较这四个用户的属性。
如果任何用户至少有一个不同的属性,则应将其添加到结果列表中。
请告诉我该如何解决这个问题?
resutList包含两个相似的用户吗? –
@Bhesh Gurung。不只是列表中的用户1. – ashishjmeshram
empcode的情况如何,他们需要匹配吗? –