比较两个列表

时间:2018-03-29 13:52:31

标签: collections java-8

我有两个Person对象列表。

Person class has the below attributes
String Name, 
Integer Age, 
String Department, 
Date CreatedTime,
String CreatedBy

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

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

2 个答案:

答案 0 :(得分: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;
    }
}

然后你可以像这样比较两个给定的列表:

 boolean result = firstList.equals(secondList);

修改

跟随你的评论:

  

我需要这种形式的比较仅用于测试目的。在我的   生产代码我想保留equals和hashcode进行比较   所有领域

您可以定义一个自定义相等的方法,如下所示:

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

或者如果你想允许将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)));
}

然后与它一起确定两个给定的人物对象是否相等的方法:

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

然后这样称呼它:

boolean result = areEqual(firstList, secondList);

答案 1 :(得分:0)

您可以使用Guava的Streams.zip()获取更实用的解决方案。忽略null检查以简洁:

public static boolean comparePersonLists(List<Person> list1, List<Person> list2) {
    if (list1.size() != list2.size()) {
        return false;
    }

    return Streams.zip(list1.stream(), list2.stream(), (a, b) ->
        a == b || (a.name.equals(b.name) && a.age.equals(b.age) && a.department.equals(b.department))
    ).allMatch(t -> t);
}