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.name
is more or less the same as Person.firstname + Person.lastname
, that you have some function in Address
like boolean checkEquality(String street1, String street2, String city, String state, String zip)
, that your Person
class has a getName()
method to compare with PersonNpi
s. In that case, loop through the first array, and check for every item if the second has anything equal to it.
ArrayList<Employee> employees = new ArrayList<Employee>();
for(Person person : personList) {
for(PersonNpi personNpi : npiList) {
if (person.getName().equals(personNpi.getName()) &&
person.getAddress().checkEquality(...address parts here...)) {
employees.add(new Employee(person, personNpi));
}
}
}
Again, I made a lot of assumptions, also the one that you have an Employee
constructor which just requires the Person
and the PersonNpi
, and gets the required information accordingly.
You should elaborate more, use superclasses, and use the contains()
function. In other words, make comparing the Person
and the PersonNpi
easier through a function.
Edit: your second question is highly, if not extremely dependant on your further implementation of Employee
, Person
and PersonNpi
. For now, I'll yet again assume you have some methods that verify equality between Employee
, Person
and PersonNpi
.
I'd suggest to not do the checking in one loop, since you have two ArrayLists
which are ran through. The PersonNpi
-list is ran through for every record in the first List
. So what might happen is after we checked everything, a few Persons
are left unmatched, and a few PersonNpis
are left unmatched, since we don't flag which Persons
and PersonNpis
we've matched.
In conclusion: for easiness' sake, just add this part:
ArrayList<Object> nonMatchedPersons = new ArrayList<Object>();
for (Person person : personList)
if (!employees.contains(person))
nonMatchedPersons.add(person);
for (PersonNpi personNpi : npiList)
if (!employees.contains(personNpi))
nonMatchedPersons.add(personNpi);
This method does require you to implement the equals(Object)
method for all 3 person classes, which you might consider putting beneath a superclass like Human
. In that case, you can make the Object ArrayList
into a ArrayList<Human>
With one loop (requires equals(Object)
method for the 3 person classes):
List<Employee> employees = new ArrayList<Employee>();
ArrayList<Object> nonMatchedPersons = new ArrayList<Object>();
Iterator<Person> personIterator = personList.iterator();
while (personIterator.hasNext()) {
Iterator<PersonNpi> npiIterator = npiList.iterator();
while(npiIterator.hasNext()) {
Person person = personIterator.next();
PersonNpi personNpi = npiIterator.next();
if (person.equals(personNpi)) {
employees.add(new Employee(person, personNpi));
personIterator.remove();
npiIterator.remove();
}
}
}
nonMatchedPersons.addAll(personList);
nonMatchedPersons.addAll(npiList);
Explanation: we loop with Iterators
through both lists, to enable us to remove from the list while iterating. So in the personList
and the npiList
, only the singles remain, as we add doubles to the Employee
-list, instantly removing them from the other two lists. We add the remaining singles in the two lists to our nonMatchedPerson
-list with the addAll
method.
Edit2: If you can't edit those classes for whatever reason, make 3 wrapper classes, something like:
public class PersonWrapper {
private Person person;
public PersonWrapper(Person person) {
this.person = person;
}
@override
public boolean equals(Object other) {
if (other == null)
return false;
if (other instanceof PersonWrapper) {
//etc etc, check for equality with other wrappers.
...
}
}
}
If you choose to use this approach, change this line in the loop:
if (person.equals(personNpi)) {
to this:
if (new PersonWrapper(person).equals(new PersonNpiWrapper(personNpi))) {
Using this, you can still implement your own equals()
method.
Another solution could be that you make a static method like this:
public static boolean equals(Object this, Object that) {
if (this instanceof Person || this instanceof PersonNpi) //et cetera, et cetera
return true;
return false;
}
Now just call Person.equals(person, personNpi)
, assuming you put the method in the class Person
.