如何比较两个数组列表的量在至少一个属性的不同之处的java相似的对象? [英] How to compare two array lists for similar objects which differ in at least one property in java?

查看:80
本文介绍了如何比较两个数组列表的量在至少一个属性的不同之处的java相似的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组列表。每个人都有类型用户的对象列表。

I have two array list. Each has list of Objects of type User.

User类看起来像下面

The User class looks like below

    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)


    }

}

如果你看到样品code,两个列表有四个用户EMP code EMP01,EMP02,EMP09和EMP10常见。

If you see the sample code, the two lists have four users with emp code EMP01, EMP02, EMP09 and EMP10 common.

所以,我们只需要比较这些四个用户的属性。

So, we only need to compare the properties of these four users.

如果任何用户至少有一个不同的属性,应该在结果列表中加入。

If any of the users have at least one different property it should be added in the result list.

请告知我怎么去呢?

推荐答案

我想这个你应该做的 -

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);
            }
        }
    }
}

它可能没有为的用户覆盖等于散$ C感$ C 只服务于这一目的。他们应该在它更有意义域明智的方式被覆盖。

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.

这篇关于如何比较两个数组列表的量在至少一个属性的不同之处的java相似的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆