重构 - 简化java中的嵌套for循环

时间:2011-03-22 22:41:58

标签: java for-loop recursion refactoring

我需要弄清楚如何改进以下代码:

      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);
                            }
                        }
                    }
                }
            }
        }

我真的很感激任何帮助简化这个“事情”

6 个答案:

答案 0 :(得分:8)

这对于递归来说看起来很棒,因为所有循环都具有完全相同的结构和内容。递归的想法是将所有循环嵌套到某个深度d,递归结构为

  • 嵌套到深度为零是一个无操作,
  • 嵌套到深度d + 1会在深度d的所有循环上执行for循环。

这可以写成

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);
}

然后,您可以致电explore进行探索。

当然,这种方法的假设是深度最多为5。如果你想消除深度要求并且只是一直探索到目录的底部,那么你可以像这样消除深度参数:

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);
    }
}

更一般地说,只要你想在任何一个内部嵌套任意数量的循环,就可以考虑递归作为一个选项。这是表达这一概念的非常通用的框架。

希望这有帮助!

答案 1 :(得分:4)

void categoryPersister(DirCategory c){
    c.setCount(dirEntryService.getDirEntryCategoryCount(c));
    log.debug("c: "+c.getCount()+" - "+c.getName());
    dirCategoryService.persist(c);
    for (DirCategory child : c.getChildren()) {
       categoryPersister(child) ;
    }
}
像这样的东西。

答案 2 :(得分:0)

你不能递归吗?你应该能够很好地将这个逻辑嵌入到一个递归调用中......你应该能够从中获得一些性能提升。这样做的另一个好处是,您的文件夹嵌套的级别无关紧要。

Wikipedia article on Recursion

答案 3 :(得分:0)

使用递归。

void handleChild( List<DirCategory> catgories) {
     if(categories == null || categories.lenth() == 0) // not sure what the condition is
        return;
     else {
       for( DirCategory cat : catgories) {
         // do stuff
          handleChild(cat.getChild())
         }

     }

}

答案 4 :(得分:0)

你必须喜欢递归:

public void persist(DirCategory category, int level) {
    category.setCount(dirEntryService.getDirEntryCategoryCount(category));
    log.debug(level + ": "+category.getCount()+" - "+category.getName());
    dirCategoryService.persist(category);
    List<DirCategory> catChildren = cateogyr.getChildren();
    for (DirCategory child : catChildren) {
      persist(child, level + 1);
    }
}       

在您的代码中:

persist(c1);

答案 5 :(得分:0)

写两种方法:

第1个用于检索DirCategory,第2个用于检索孩子。像这样:

private void retrieveDirCategory(DirCategory c) {
    c.setCount(dirEntryService.getDirEntryCategoryCount(c));
    log.debug("c: "+c.getCount()+" - "+c.getName());
    dirCategoryService.persist(c);      
}

private void retrieveDeep(Collections<DirCategory> categories, int deep) {
    if (deep == 0) {
        return;
    }

    for (DirCategory c : categories) {
        retrieveDirCategory(c);
        retrieveDeep(c.getChildren(), deep-1);
    }
}
// and you call:
// retrieveDeep(categories1, 6);