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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:04:55+00:00 2026-06-13T17:04:55+00:00

I have a list of IDs: List<Integer> updatedIds . I have a master list

  • 0

I have a list of IDs: List<Integer> updatedIds.

I have a master list (say, taken from the DB): List<Records> masterList.

I want to do the following:

  1. For each ID in updatedIds, check if it’s in masterList. If not, add the record to the masterList.
  2. For each Record in masterList, check if it’s in updatedIds. If not, it is obsolete, so remove it from masterList.

The straightforward code for this is as follows:

for (Integer updId : updatedIds) {
    boolean hasMapping = false;
    for (Record rec : masterList) {
        if (rec.getId() == updId) { hasMapping = true; break; }
    }
    if (!hasMapping) {
        //TODO add updId to masterList
    }
}
for (Record rec : masterList) {
    boolean isObsolete = true;
    for (Integer updId : updatedIds) {
        if (rec.getId() == updId) { isObsolete = false; break; }
    }
    if (isObsolete) {
        //TODO remove rec from masterList
    }
}

The first loop takes care of requirement 1, the second takes care of requirement 2. It looks very inefficient, and I think I may be using the wrong data structure for this kind of task.

Is there a more efficient way of implementing the algorithm above?

  • 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-13T17:04:57+00:00Added an answer on June 13, 2026 at 5:04 pm

    If you sort both lists (e.g. using Collections.sort), the updatedIDs in natural order and the masterList ordered by ID, you can set up a single loop to go through both of them. You could possibly retrieve the records in sorted order, if they come from a DB, and skip that step.

    Collections.sort(masterList, myComparator);
    Collections.sort(updatedIDs);
    
    Iterator m_it = masterList.iterator();
    Iterator u_it = updatedIDs.iterator();
    
    // * Some code here to deal with the possibility that either list is empty
    
    Record rec    = m_it.next();
    int    u      = u_it.next();
    bool   done   = false;
    
    while (! done) {
      if (rec.getID() < u) {
        // rec's ID was missing from updatedIDs
        m_it.remove();
    
        if (m_it.hasNext()) {
          rec = m_it.next();
        } else {
          done = true;
          // * add u and all subsequent updated IDs to master list
        }
      } else if (rec.getID() > u) {
        // u is new - doesn't occur in masterList
        // * add u to masterList (or probably to a separate list that you
        //   later append to masterList)
    
        if (u_it.hasNext()) {
          u = u_it.next();
        } else {
          done = true;
          // * remove rec and all remaining records from the master list
        }
      } else {
        // rec's ID matches u: proceed to next pair of items
        bool m_nx = m_it.hasNext(), u_nx = u_it.hasNext();
        if (m_nx && u_nx) {
          rec = m_it.next();
          u = u_it.next();
        } else if ((! m_nx) && (! u_nx)) {
          done = true;
        } else if (m_nx && (! u_nx)) {
          done = true;
          // * remove all subsequent records from the master list
        } else if ((! m_nx) && u_nx) {
          done = true;
          // * add all subsequent integers in updatedIDs to the master list
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a list of event Ids that i want to be excluded from
I have to pull a list of integer IDs from a table using only
I have a a method like this : List < Object > getObjects(List<Integer> ids)
I have a comma delimited list of ids that I want to use to
I have a list of product ids and I want to get all the
I have a list of items (containing integer ids). I need to delete all
I have a list of numbers: 7,1,3,2,123,55 (which are the ids of existing records)
I have a list of IDs for objects that I need to grab, then
if I have a list of IDs (1,4,6,7) and a db table where I
I have a list of parent/child IDs and would like to get all child

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.