比较两个列表时忽略字段

ser*_*rah 5 collections java-8

我有两个 Person 对象列表。

Person class has the below attributes
String Name, 
Integer Age, 
String Department, 
Date CreatedTime,
String CreatedBy
Run Code Online (Sandbox Code Playgroud)

当我比较列表是否相等时,我不想比较 CreatedTime 和 CreatedBy 字段。

如何使用 Java 8 比较两个列表的相等性,并忽略 CreatedTime、CreatedBy 字段进行比较?

Ous*_* D. 2

您甚至不需要 Java-8 的功能来执行此操作,只需像这样覆盖equals&hashCode即可:

class Person {
    String name;
    Integer age;
    String department;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        if (name != null ? !name.equals(person.name) : person.name != null) return false;
        if (age != null ? !age.equals(person.age) : person.age != null) return false;
        return department != null ? department.equals(person.department) : person.department == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (age != null ? age.hashCode() : 0);
        result = 31 * result + (department != null ? department.hashCode() : 0);
        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后您可以比较两个给定的列表,如下所示:

 boolean result = firstList.equals(secondList);
Run Code Online (Sandbox Code Playgroud)

编辑:

根据您的评论:

我仅出于测试目的才需要这种形式的比较。在我的生产代码中,我想保留 equals 和 hashcode 来比较所有字段

您可以像这样定义自定义 equal 方法:

public static boolean areEqual(List<Person> first, List<Person> second) {
        Objects.requireNonNull(first, "first list must not be null");
        Objects.requireNonNull(second, "second list must not be null");
        return first.size() == second.size() && 
                IntStream.range(0, first.size()).allMatch(index -> 
                        customCompare(first.get(index), second.get(index)));
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您想允许将 null 传递给该areEqual方法,那么稍作更改就足够了:

public static boolean areEqual(List<Person> first, List<Person> second){
        if (first == null && second == null)
            return true;
        if(first == null || second == null ||
                first.size() != second.size()) return false;

        return IntStream.range(0, first.size())
                        .allMatch(index -> 
       customCompare(first.get(index), second.get(index)));
}
Run Code Online (Sandbox Code Playgroud)

然后是确定两个给定的 person 对象是否相等的方法:

static boolean customCompare(Person firstPerson, Person secondPerson){

        if (firstPerson == secondPerson) return true;

        if (firstPerson.getName() != null
                ? !firstPerson.getName().equals(secondPerson.getName()) : secondPerson.getName() != null)
            return false;

        return (firstPerson.getAge() != null ? firstPerson.getAge().equals(secondPerson.getAge()) : secondPerson.getAge() == null)
                && (firstPerson.getDepartment() != null
                ? firstPerson.getDepartment().equals(secondPerson.getDepartment())
                : secondPerson.getDepartment() == null);
}
Run Code Online (Sandbox Code Playgroud)

然后像这样调用它:

boolean result = areEqual(firstList, secondList);
Run Code Online (Sandbox Code Playgroud)