We need to compare 2 arraylists of different objects having some common fields, and then store the matching rows to a new arraylist. I have searched for solutions, but wasn’t able to get what I need.
List<Person> personList = new ArrayList<Person>();
Person:
private String firstName;
private String lastName;
private String street1;
private String street2;
private String city;
private String stateCode;
private String zipCode;
List<PersonNpi> npiList = new ArrayList<PersonNpi>();
PersonNpi:
private String name;
private String npi;
private Address address;
So I need to check if the name & address in the PersonNpi object in the PersonNpiList match to a Person object in the PersonList, and if yes save the Person details + Npi to a new Arraylist<Employee>
Hope I’m clear on the question. Please let me know on how to solve this efficiently.
Thanks
Harry
EDIT:
I need to save the non-matching rows (on the first arraylist) as well to another list. Do I need to have another loop or can I do it on the same For loop? Anyone please?
Since I don’t see any superclasses from which they extend, you have to manually iterate through your lists. I am assuming a lot, for instance that you have getters and setters for your attributes, that
PersonNpi.nameis more or less the same asPerson.firstname + Person.lastname, that you have some function inAddresslikeboolean checkEquality(String street1, String street2, String city, String state, String zip), that yourPersonclass has agetName()method to compare withPersonNpis. In that case, loop through the first array, and check for every item if the second has anything equal to it.Again, I made a lot of assumptions, also the one that you have an
Employeeconstructor which just requires thePersonand thePersonNpi, and gets the required information accordingly.You should elaborate more, use superclasses, and use the
contains()function. In other words, make comparing thePersonand thePersonNpieasier through a function.Edit: your second question is highly, if not extremely dependant on your further implementation of
Employee,PersonandPersonNpi. For now, I’ll yet again assume you have some methods that verify equality betweenEmployee,PersonandPersonNpi.I’d suggest to not do the checking in one loop, since you have two
ArrayListswhich are ran through. ThePersonNpi-list is ran through for every record in the firstList. So what might happen is after we checked everything, a fewPersonsare left unmatched, and a fewPersonNpisare left unmatched, since we don’t flag whichPersonsandPersonNpiswe’ve matched.In conclusion: for easiness’ sake, just add this part:
This method does require you to implement the
equals(Object)method for all 3 person classes, which you might consider putting beneath a superclass likeHuman. In that case, you can make theObject ArrayListinto aArrayList<Human>With one loop (requires
equals(Object)method for the 3 person classes):Explanation: we loop with
Iteratorsthrough both lists, to enable us to remove from the list while iterating. So in thepersonListand thenpiList, only the singles remain, as we add doubles to theEmployee-list, instantly removing them from the other two lists. We add the remaining singles in the two lists to ournonMatchedPerson-list with theaddAllmethod.Edit2: If you can’t edit those classes for whatever reason, make 3 wrapper classes, something like:
If you choose to use this approach, change this line in the loop:
to this:
Using this, you can still implement your own
equals()method.Another solution could be that you make a static method like this:
Now just call
Person.equals(person, personNpi), assuming you put the method in the classPerson.