2 つの配列リストがあります。それぞれにタイプ User のオブジェクトのリストがあります。
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", "u1@test.com"));
list1.add(new User("F2", "L2", "EMP02", "u222@test.com"));
list1.add(new User("F3", "L3", "EMP03", "u3@test.com"));
list1.add(new User("F4", "L4", "EMP04", "u4@test.com"));
list1.add(new User("F5", "L5", "EMP05", "u5@test.com"));
list1.add(new User("F9", "L9", "EMP09", "u9@test.com"));
list1.add(new User("F10", "L10", "EMP10", "u10@test.com"));
List<User> list2 = new ArrayList<User>();
list2.add(new User("F1", "L1", "EMP01", "u1@test.com"));
list2.add(new User("F2", "L2", "EMP02", "u2@test.com"));
list2.add(new User("F6", "L6", "EMP06", "u6@test.com"));
list2.add(new User("F7", "L7", "EMP07", "u7@test.com"));
list2.add(new User("F8", "L8", "EMP08", "u8@test.com"));
list2.add(new User("F9", "L9", "EMP09", "u9@test.com"));
list2.add(new User("F100", "L100", "EMP10", "u100@test.com"));
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)
}
}
サンプル コードを見ると、2 つのリストには emp コード EMP01、EMP02、EMP09、および EMP10 が共通の 4 人のユーザーが含まれています。
したがって、これら 4 人のユーザーのプロパティを比較するだけで済みます。
いずれかのユーザーが少なくとも 1 つの異なるプロパティを持っている場合、結果リストに追加する必要があります。
これについてどうすればよいかアドバイスしてください。