How to compare two array lists for similar objects which differ in at least one property in java?

前端 未结 6 1944
眼角桃花
眼角桃花 2021-01-07 01:03

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         


        
相关标签:
6条回答
  • 2021-01-07 01:17

    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));
    }
    
    0 讨论(0)
  • 2021-01-07 01:21

    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);
        }
     }
    
    0 讨论(0)
  • 2021-01-07 01:25

    The canonical approach is as follows:

    1. Write a method countDifferences that counts the number of differences between users
    2. For each object in one list, find the minimum when compared to the other lists objects
    3. Report all objects where the minimum is not 0.

    If 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.

    0 讨论(0)
  • 2021-01-07 01:27

    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.

    0 讨论(0)
  • 2021-01-07 01:31

    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
    
    0 讨论(0)
  • 2021-01-07 01:38

    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);                  
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题