Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload