I have two array list. Each has list of Objects of type User.
The User class looks like below
public class User {
private long id;
pri
I have used the following way to compare two custom ArrayList
.
List<SinglePostData> allPosts = new ArrayList<>();
List<SinglePostData> noRepeatAllPosts = new ArrayList<>();
for (int i = 0; i < allPosts.size(); i++) {
boolean isFound = false;
for (int j = i+1; j < allPosts.size(); j++) {
if (allPosts.get(i).getTitle().equals(allPosts.get(j).getTitle())) {
isFound = true;
break;
}
}
if (!isFound) noRepeatAllPosts.add(allPosts.get(i));
}
This is simple. Override equal
method in your User
class. One very simple implementation(you can enhance it by using null checks etc) can be like below:
@override
public boolean equals(Object obj) {
User other = (User) obj;
if(this.id==other.id
&& this.empCode.equals(other.empCode)
&& this.firstname.equals(other.firstname)
&& this.lastname.equals(other.lastname)
&& this.email.equals(other.email)){
return true;
}else{
return false;
}
}
Once done, you can use:
for(user user: list1){
if(!resultList.contains(user)){
resultList.add(user);
}
}
for(user user: list2){
if(!resultList.contains(user)){
resultList.add(user);
}
}
The canonical approach is as follows:
countDifferences
that counts the number of differences between usersIf you put weights on the different properties you can also control that e.g. a match in the ID attribute is stronger than a match in the name.
Update: sorry, misread your comment that the ID attribute must match.
Replace 2) with "find object which has the same ID". Other than that, I still recommend counting the number of differences. It is more flexible, as you can define thresholds for good or bad matches etc.
I think this what you should do -
for(User user1 : list1) {
for(User user2 : list2) {
if(user1.getEmpCode().equals(user2.getEmpCode())) {
if(!user1.getFirstName().equals(user2.getFirstName()) ||
!user1.getLastName().equals(user2.getLastName()) ||
!user1.getEmail().equals(user2.getEmail())) {
resultList.add(user1);
}
}
}
}
It might not make sense for the User
to override equal
and hashCode
only to serve this purpose. They should be overriden in the way in which it makes more sense domain-wise.
Implement equals, hashcode in User
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (!(obj instanceof User))
return false;
User u = (User) obj;
return this.empCode == null ? false : this.empCode
.equals(u.empCode);
}
@Override
public int hashCode() {
return this.empCode == null ? 0 : this.empCode.hashCode();
}
@Override
public String toString() {
return "Emp Code: " + this.empCode;
}
Then use retainAll
list2.retainAll(list1);-->EMP01, EMP02, EMP09, EMP10
Add this method to your User class:
public boolean isSimilarButNotEqual(User other) {
if(!this.empCode.equals(other.empCode))
return false;
return !(this.firstname + this.lastname + this.email).equals(other.firstname + other.lastname + other.email);
}
Then, in the main()
do:
for(User user1: list1){
for(User user2: list2){
if(user1.isSimilarButNotEqual(user2)){
resultList.add(user1);
resultList.add(user2);
}
}
}