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

  • SEARCH
  • Home
  • 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 7749313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:00:57+00:00 2026-06-01T11:00:57+00:00

I need small help. I have 2 lists( say A and B) which have

  • 0

I need small help. I have 2 lists( say A and B) which have objects of a user defined class.

Both A and B have a method getId() which would return an integer (ID). Now, I need to compare the ID of every object in List A with each and every object of List B. If the ID is same then I have to replace that object from B with that of A.

If the id of the object from A is not in B then I would add that object to B

Please provide me a logic on how to achive this

Thanks

  • 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-01T11:00:58+00:00Added an answer on June 1, 2026 at 11:00 am

    Something like this would work, assuming your list is an arrayList with the possibility to access elements with an index

    for(int i=0;i<a.size();i++)
        for(int j=0;j<b.size();j++)
           if(a.get(i).getId().equals(b.get(j).getId()){
             //this id from A exists in B. Replace
             b.get(j)=a.get(i);
           }
           else{
              if(j=(b.size()+1)){ // if true the whole b list have been searched
                 //object not found. Add it to b
                 b.add(a.get(i));      
              } 
    }
    

    This requiers that your elements in a and b override the equals statment, so they can compare the ID with eachother

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

Sidebar

Related Questions

I need some help adding conditions to a linq query. Take this small example
I need to design a small project for generating excel reports in .NET, which
Please help me out with this. I have this small application to load txt
I have written some code in which a small part of the code takes
I need my Android Listview to display a Help screen as a header which
I have a static class 'Defaults' which shall hold default matrices that are forwarded
I am working on a small Android App! I have a class MyPref public
I have this script which works except for one small problem. Basically it gets
We're having a small issue and could use some help - we have the
I need small help with jquery. EDIT: echo <li> ; echo $navi_menu->navi_name; echo <ul>;

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.