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

重构-简化java中的嵌套for循环,java,for-loop,recursion,refactoring,Java,For Loop,Recursion,Refactoring,我需要找出如何改进以下代码: for (DirCategory c1 : categories1) { c1.setCount(dirEntryService.getDirEntryCategoryCount(c1)); log.debug("c1: "+c1.getCount()+" - "+c1.getName()); dirCategoryService.persist(c1);

我需要找出如何改进以下代码:

      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);
                            }
                        }
                    }
                }
            }
        }
for(目录c1:categories1){
c1.setCount(dirEntryService.getDirEntryCategoryCount(c1));
log.debug(“c1:+c1.getCount()+”-“+c1.getName());
dirCategoryService.persist(c1);
List categories2=c1.getChildren();
对于(类别c2:类别2){
c2.setCount(dirEntryService.getDirEntryCategoryCount(c2));
log.debug(“c2:+c2.getCount()+”-“+c2.getName());
dirCategoryService.persist(c2);
List categories3=c2.getChildren();
对于(类别c3:类别3){
c3.setCount(dirEntryService.getDirEntryCategoryCount(c3));
log.debug(“c3:+c3.getCount()+”-“+c3.getName());
dirCategoryService.persist(c3);
List categories4=c3.getChildren();
对于(类别c4:类别4){
c4.setCount(dirEntryService.getDirEntryCategoryCount(c4));
log.debug(“c4:+c4.getCount()+”-“+c4.getName());
dirCategoryService.persist(c4);
List categories5=c4.getChildren();
对于(类别c5:类别5){
c5.setCount(dirEntryService.getDirEntryCategoryCount(c5));
log.debug(“c5:+c5.getCount()+”-“+c5.getName());
dirCategoryService.persist(c5);
List categories6=c5.getChildren();
对于(类别c6:类别6){
setCount(dirEntryService.getDirEntryCategoryCount(c6));
log.debug(“c6:+c6.getCount()+”-“+c6.getName());
dirCategoryService.persist(c6);
}
}
}
}
}
}

如果能帮我简化这件事,我将不胜感激。

你不能递归地做吗?您应该能够很好地将此逻辑嵌套到递归调用中。。。您也应该能够从中获得一些性能增益。这样做的另一个好处是,不管文件夹嵌套了多少层


您不能递归地执行此操作吗?您应该能够很好地将此逻辑嵌套到递归调用中。。。您也应该能够从中获得一些性能增益。这样做的另一个好处是,不管文件夹嵌套了多少层

像这样的


类似于此。

这对于递归来说似乎是一项伟大的工作,因为所有循环都具有完全相同的结构和内容。递归思想是将所有循环嵌套到某个深度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);
}
更一般地说,任何时候你想把任意数量的循环嵌套在一起,考虑递归是一种选择。这是表达这个概念的一个非常普遍的框架


希望这有帮助

对于递归来说,这似乎是一项伟大的工作,因为所有循环都具有完全相同的结构和内容。递归思想是将所有循环嵌套到某个深度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);
}
更一般地说,任何时候你想把任意数量的循环嵌套在一起,考虑递归是一种选择。这是表达这个概念的一个非常普遍的框架

希望这有帮助

使用递归

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

     }

}
void handleChild(列出类别){
if(categories==null | | categories.lenth()==0)//不确定条件是什么
回来
否则{
适用于(类别:猫类){
//做事
handleChild(cat.getChild())
}
}
}
使用递归

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

     }

}
void handleChild(列出类别){
if(categories==null | | categories.lenth()==0)//不确定条件是什么
回来
否则{
适用于(类别:猫类){
//做事
handleChild(cat.getChild())
}
}
}

你一定喜欢递归:

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

你必须热爱递归:

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);
    }
}       
写两种方法:

第一个用于检索目录,第二个用于检索子目录。大概是这样的:

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);
    }
}
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);
private void retrievedirectory(目录c){
c、 setCount(dirEntryService.getDirEntryCategoryCount(c));
调试(“c:+c.getCount()+”-“+c.getName());
dirCategoryService.persist(c);
}
私有void retrieveDeep(集合类别,int deep){
如果(深度==0){
回来
}
对于(DIRC类别:类别){
检索目录(c);
retrieveDeep(c.getChildren(),deep-1);
}
}
//你打电话:
//可检索的eep(第1、6类);
编写两种方法:

第一个用于检索目录,第二个用于检索子目录。大概是这样的:

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);
    }
}
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);
private void retrievedirectory(目录c){
c、 setCount(dirEntryService.getDirEntryCategoryCount(c));
调试(“c:+c.getCount()+”-“+c.getName());
dirCategoryService.persist(c);
}
私有void retrieveDeep(集合类别,int deep){
如果(深度==0){
回来
}
对于(DIRC类别:类别){
检索目录(c);
retrieveDeep(c.getChildren(),deep-1);
}
}
//你打电话:
//可检索的eep(第1、6类);

OMG多么疯狂的代码…我的