Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8935563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:00:57+00:00 2026-06-15T10:00:57+00:00

We need to compare 2 arraylists of different objects having some common fields, and

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:00:58+00:00Added an answer on June 15, 2026 at 10:00 am

    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 PersonNpis. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to compare dozens of fields in two objects (instances of the same
Possible Duplicate: How to compare two arraylist? I have two String ArrayLists of different
I have an ArrayList of objects that I need to sort in two different
I need to compare particular content of 2 SQL tables located in different servers:
I need to compare two Unix timestamps and I'm having trouble with the math.
hi i have more than 10 arraylist in my project, i need to compare
I'm having a problem with multi-threading in Java. I need to compare a large
I have an arrayList of different types of players based on sports. I need
I have some problem to compare values from different array list From this values,
I need to compare current day with some value. Like from front end I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.