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 8693121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:28:28+00:00 2026-06-13T00:28:28+00:00

I have two array list. Each has list of Objects of type User. The

  • 0

I have two array list. Each has list of Objects of type User.

The User class looks like below

    public class User {

    private long id;

    private String empCode;

    private String firstname;

    private String lastname;

    private String email;

    public User( String firstname, String lastname, String empCode, String email) {
        super();
        this.empCode = empCode;
        this.firstname = firstname;
        this.lastname = lastname;
        this.email = email;
    }

    // getters and setters

}


    import java.util.ArrayList;
import java.util.List;

public class FindSimilarUsersWithAtLeastOneDifferentProperty {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        List<User> list1 = new ArrayList<User>();

        list1.add(new User("F11", "L1", "EMP01", "u1@test.com"));
        list1.add(new User("F2", "L2", "EMP02", "u222@test.com"));
        list1.add(new User("F3", "L3", "EMP03", "u3@test.com"));
        list1.add(new User("F4", "L4", "EMP04", "u4@test.com"));
        list1.add(new User("F5", "L5", "EMP05", "u5@test.com"));
        list1.add(new User("F9", "L9", "EMP09", "u9@test.com"));
        list1.add(new User("F10", "L10", "EMP10", "u10@test.com"));

        List<User> list2 = new ArrayList<User>();

        list2.add(new User("F1", "L1", "EMP01", "u1@test.com"));
        list2.add(new User("F2", "L2", "EMP02", "u2@test.com"));
        list2.add(new User("F6", "L6", "EMP06", "u6@test.com"));
        list2.add(new User("F7", "L7", "EMP07", "u7@test.com"));
        list2.add(new User("F8", "L8", "EMP08", "u8@test.com"));
        list2.add(new User("F9", "L9", "EMP09", "u9@test.com"));
        list2.add(new User("F100", "L100", "EMP10", "u100@test.com"));

        List<User> resultList = new ArrayList<User>();
        // this list should contain following users
        // EMP01 (common in both list but differs in firstname)
        // EMP02 (common in both list but differs in email)
        // EMP10 (common in both list but differs in firstname, lastname and email)


    }

}

If you see the sample code, the two lists have four users with emp code EMP01, EMP02, EMP09 and EMP10 common.

So, we only need to compare the properties of these four users.

If any of the users have at least one different property it should be added in the result list.

Please advise on how do I go about this?

  • 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-13T00:28:30+00:00Added an answer on June 13, 2026 at 12:28 am

    I think this what you should do –

    for(User user1 : list1) {
        for(User user2 : list2) {
            if(user1.getEmpCode().equals(user2.getEmpCode())) {
                if(!user1.getFirstName().equals(user2.getFirstName()) ||
                   !user1.getLastName().equals(user2.getLastName()) ||
                   !user1.getEmail().equals(user2.getEmail())) {
                    resultList.add(user1);
                }
            }
        }
    }
    

    It might not make sense for the User to override equal and hashCode only to serve this purpose. They should be overriden in the way in which it makes more sense domain-wise.

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

Sidebar

Related Questions

I have two array list. Each has list of Objects of type Employee. The
I have two array list. Each has list of Objects of type Employee. The
I have a manager class maintaining a list of objects. Each Object has a
So we have a java class with two ArrayLists of generics. It looks like
I have two arrays, each containing strings. The first array is a list of
I have an array of structs. The struct has two function pointers. Each element
I have two std::list , which each one has a positional vector m_Pos .
I have a list of type List<Element<Integer, Integer>> , where each element contains two
I have two different arrays. One array, a, for a list of people. My
I have two List of array string. I want to be able to create

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.