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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:21:24+00:00 2026-06-09T00:21:24+00:00

I have a two lists. List A and List B . Each element of

  • 0

I have a two lists. List A and List B.

Each element of list A has a list of elements from List B. I have populated the two lists but population of the List within the element of List A is still to be done.

They both have a property on which the linkage can be done; say

an element from List B; if has the field 1 as the id of the element from List A, then it gets added to that element from List A.

Now i am currently doing it using a for loop within another for loop. Somethign like this

for(each A)
{
for(each B)
{
if(fieldsmatch)
{
add B to the List of the element from A
}
}

Will it be better if I Hash all the elements from List A and then perform lookups while going through each element from List B.

Thanks.

Can i use HashMap for 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-09T00:21:26+00:00Added an answer on June 9, 2026 at 12:21 am

    I’d reverse the logic. Create a hashmap from B that maps each id to a list of elements that have that id. Then for each element of A, look up the list by id and assign it to the field in the A element.

    Map<IdType, List<B>> map = new HashMap<IdType, List<B>>();
    for(each b in B) {
        List<B> list = map.get(b.id);
        if (list == null) {
            list = new ArrayList<B>();
            map.put(b.id, list);
        }
        list.add(b);
    }
    for(each a in A) {
        a.b_list = map.get(a.id);
        // if you need an empty list instead of null:
        if (a.b_list == null) {
            a.b_list = new ArrayList<B>();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two lists. They are sortable but not connected (elements of list one
I have two ordered lists of the same element type, each list having at
I have implemented a list view. Each item in my list view has two
We have two lists: a=['1','2','3','4'] b=['2','3','4','5'] How to get a list with elements that
I have an array of structs. The struct has two function pointers. Each element
I have a list of type List<Element<Integer, Integer>> , where each element contains two
I have two list of different objects : List<Report> List<Newsletter> each having a 'created
I have a list. Each row includes an Image at left and two text
i have two lists: List<comparerobj> list_c = new List<comparerobj>(); List<comparerobj> list_b = new List<comparerobj>();
Ok so I have two lists in C# List<Attribute> attributes = new List<Attribute>(); List<string>

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.