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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:15:15+00:00 2026-05-20T19:15:15+00:00

I need to figure out how to improve following code: for (DirCategory c1 :

  • 0

I need to figure out how to improve following code:

      for (DirCategory c1 : categories1) {
            c1.setCount(dirEntryService.getDirEntryCategoryCount(c1));
            log.debug("c1: "+c1.getCount()+" - "+c1.getName());
            dirCategoryService.persist(c1);

            List<DirCategory> categories2 = c1.getChildren();
            for (DirCategory c2 : categories2) {
                c2.setCount(dirEntryService.getDirEntryCategoryCount(c2));
                log.debug("  c2: "+c2.getCount()+" - "+c2.getName());
                dirCategoryService.persist(c2);

                List<DirCategory> categories3 = c2.getChildren();
                for (DirCategory c3 : categories3) {
                    c3.setCount(dirEntryService.getDirEntryCategoryCount(c3));
                    log.debug("    c3: "+c3.getCount()+" - "+c3.getName());
                    dirCategoryService.persist(c3);

                    List<DirCategory> categories4 = c3.getChildren();
                    for (DirCategory c4 : categories4) {
                        c4.setCount(dirEntryService.getDirEntryCategoryCount(c4));
                        log.debug("      c4: "+c4.getCount()+" - "+c4.getName());
                        dirCategoryService.persist(c4);

                        List<DirCategory> categories5 = c4.getChildren();
                        for (DirCategory c5 : categories5) {
                            c5.setCount(dirEntryService.getDirEntryCategoryCount(c5));
                            log.debug("        c5: "+c5.getCount()+" - "+c5.getName());
                            dirCategoryService.persist(c5);

                            List<DirCategory> categories6 = c5.getChildren();
                            for (DirCategory c6 : categories6) {
                                 c6.setCount(dirEntryService.getDirEntryCategoryCount(c6));
                                log.debug("          c6: "+c6.getCount()+" - "+c6.getName());
                                 dirCategoryService.persist(c6);
                            }
                        }
                    }
                }
            }
        }

I would really appreciate any help simplifying this “thing”

  • 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-05-20T19:15:15+00:00Added an answer on May 20, 2026 at 7:15 pm

    This looks like a great job for recursion, since all of the loops have exactly the same structure and content. The recursive idea is to nest all the loops to some depth d, with the recursive structure being

    • Nesting to depth zero is a no-op, and
    • Nesting to depth d + 1 does a for loop over all loops of depth d.

    This could be written as

    private static void recursiveExplore(List<DirCategory> categories, int depth) {
        if (depth == 0) return;
    
        for (DirCategory c1 : categories) {
            c1.setCount(dirEntryService.getDirEntryCategoryCount(c1));
            log.debug("c1: "+c1.getCount()+" - "+c1.getName());
            dirCategoryService.persist(c1);
    
            recursiveExplore(c1.getChildren(), depth - 1);
        }
    }
    public static void explore(List<DirCategory> categories) {
        recursiveExplore(categories, 5);
    }
    

    You can then do the exploration by calling explore.

    Of course, this approach works with the assumption that the depth is at most five. If you want to eliminate the depth requirement and just explore all the way down to the bottom of the directory, then you can just eliminate the depth parameter like this:

    public static void explore(List<DirCategory> categories) {
        for (DirCategory c1 : categories) {
            c1.setCount(dirEntryService.getDirEntryCategoryCount(c1));
            log.debug("c1: "+c1.getCount()+" - "+c1.getName());
            dirCategoryService.persist(c1);
    
            recursiveExplore(c1.getChildren(), depth - 1);
        }
    }
    

    More generally, any time you want to nest an arbitrary number of loops inside of one another, consider recursion as an option. It’s a very general framework for expressing this concept.

    Hope this helps!

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

Sidebar

Related Questions

I need to figure out the code on my .htaccess for the following. Redirect
I need to figure out how to do some C# code in php, and
I need to figure out what div is visible out of four possible divs
I need to figure out the hard drive name for a solaris box and
I need to figure out how to get the data from D3D textures and
I UIButton using + buttonWithType: What I need to figure out is how to
I'm working on a web-application and need to figure out the best way to
I have an ASP.NET page where I need to figure out where the style
I suck at math. I need to figure out how to calculate a video
I have Windows Vista MCML app, and I need to figure out the current

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.