简体   繁体   中英

How to Compare Two Lists of Java Objects by Property Values

I want to assert that two lists of objects of the same type are the same by the property values of that object. For example, I have two lists of Person objects which have first, last, and middle name properties. Each Person object has method setters and getters for each of these properties (java beans), and the constructor contains no parameters.

Let's assume here that I already have a list of two People called existingPersonList which has the following properties:

Existing person1: firstName = Bob , lastName = Smith
Existing person2: firstName = Sue , middleName = K. , lastName = White

I want to compare the objects, so the approach I have taken is to create new ones to compare against:

Person person1 = new Person(); 
Person person2 = new Person();
List<Person> personList = new ArrayList<>();
person1.setFirstName("Bob");
person1.setLastName("Smith");
person2.setFirstName("Sue");
person2.setMiddleName("K.");
person2.setLastName("White");
personList.add(person1);
personList.add(person2);

What I'm currently doing is iterating through both lists with for loops and using the Hamcrest method samePropertyValue(personList.get(i)).matches(existingPersonList.get(j)) to find a match and assertTrue, however this is very inefficient for large lists.

Is there a better way to go about doing this? Or at very least directly compare the two lists instead of iterating through?

By overriding the equal and hash methods for your object,you can use the compare method of your lists as shows in the following sample:

this helps you to compare objects by as many as their proprieties as you want.

       import java.util.*;

    public class TestListConvert {

    public static void main(String[] args) {
        List<Obs> firstList = new ArrayList<>();

        List<Obs> secondList = new ArrayList<>();

        firstList.add(new Obs("1","2"));
        secondList.add(new Obs("1","2"));

        System.out.println(firstList.equals(secondList));

    }
    private static class Obs{
        private  String x;
        private String y;

        public Obs(String x, String y) {
            this.x = x;
            this.y = y;
        }

        public String getX() {
            return x;
        }

        public void setX(String x) {
            this.x = x;
        }

        public String getY() {
            return y;
        }

        public void setY(String y) {
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Obs obs = (Obs) o;
            return Objects.equals(x, obs.x) &&
                    Objects.equals(y, obs.y);
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }
    }
}

I want to assert that two lists of objects of the same type are the same by the property values of that object. For example, I have two lists of Person objects which have first, last, and middle name properties. Each Person object has method setters and getters for each of these properties (java beans), and the constructor contains no parameters.

Let's assume here that I already have a list of two People called existingPersonList which has the following properties:

Existing person1: firstName = Bob , lastName = Smith
Existing person2: firstName = Sue , middleName = K. , lastName = White

I want to compare the objects, so the approach I have taken is to create new ones to compare against:

Person person1 = new Person(); 
Person person2 = new Person();
List<Person> personList = new ArrayList<>();
person1.setFirstName("Bob");
person1.setLastName("Smith");
person2.setFirstName("Sue");
person2.setMiddleName("K.");
person2.setLastName("White");
personList.add(person1);
personList.add(person2);

What I'm currently doing is iterating through both lists with for loops and using the Hamcrest method samePropertyValue(personList.get(i)).matches(existingPersonList.get(j)) to find a match and assertTrue, however this is very inefficient for large lists.

Is there a better way to go about doing this? Or at very least directly compare the two lists instead of iterating through?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM