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