Optimize multiple nested for loops? [closed]

112
July 04, 2022, at 06:00 AM
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 5 months ago.

Improve this question

Is there a way to optimize the code that has multiple nested for loop? If I understand it correctly, I believe time complexity for this one is O(N^m) where m is the number of loops.

List<A> aList= getAList();
for(A a : aList){
    for(B b : a.getBList()){
          for(C c: b.getCList()){
               for(D d : c.getDList()){
                      //and so on
                      //output some fields of a b c d ...
                  }
            }
        }
   }

To make it little clear, I give an example below. Let us say I am limited to just reading the data.

@Autowired
IData data;
public void readInputFromUI(String id){
    List<Company> companies= data.getList(Company.class, id);
    for(Company c: companies){
        for(Department d : data.getList(Department.class, c.getId()){
            for(Team t: data.getList(Team.class, d.getId()){
                 for(Employee e : data.getList(Employee.class, t.getId()){
                      //and so on...
                      //output some fields of c d t e...
                  }
            }
        }
     }
   }

The relationship between the outside loop and 1 level lower loop entities are bi-directional. I can get team from employee, department from team and so on.. For instance,

Team t = e.getTeam();
Department d = t.getDepartment();
Company c = d.getCompany();
Rent Charter Buses Company
READ ALSO
Priority Queue - Order Messed Up

Priority Queue - Order Messed Up

I'm having a weird issue where my priority queue prints the last created item first, and then prints everything else in order

66
jMonkeyEngine dependencies with Gradle

jMonkeyEngine dependencies with Gradle

I am currently trying to implement a game written in java using jMonkey Graphic engineMy question is if it is possibile to load all the dependencies needed through Gradle, avoiding all *

102
package com.sun.beans.finder does not exist openjdk

package com.sun.beans.finder does not exist openjdk

When a file is compiled that imports comsun

114
Why does the JScrollPane show up but not its scroll bar? [duplicate]

Why does the JScrollPane show up but not its scroll bar? [duplicate]

Here is my codeThe JScrollpanel shows up but now the scrollbar

91