需要在Java中增强嵌套for循环的建议吗

需要在Java中增强嵌套for循环的建议吗,java,for-loop,java-8,spring-data,spring-data-jpa,Java,For Loop,Java 8,Spring Data,Spring Data Jpa,在我的应用程序中,我有4个级别的类别,每个类别将有一组7个子类别。所以它将是一个(7*7*7)结构。我需要在一次服务呼叫中获取所有四个级别的类别。我的表格结构如下: categoryId, categoryName, parentId, active List<DrmCategory> cate1 = categoryRepository.findByParentId(0); cate1.forEach(category -> {

在我的应用程序中,我有4个级别的类别,每个类别将有一组7个子类别。所以它将是一个(7*7*7)结构。我需要在一次服务呼叫中获取所有四个级别的类别。我的表格结构如下:

categoryId, categoryName, parentId, active
 List<DrmCategory> cate1 = categoryRepository.findByParentId(0);
        cate1.forEach(category -> {
             System.out.println(" Level1 -> Parent ID : "+category.getParentId()+ " Name : "+category.getCategoryName());

             List<DrmCategory> cate2 = categoryRepository.findByParentId(category.getCategoryId());
             cate2.forEach(category2 ->{
                 System.out.println(" Level2 -> Parent ID : "+category2.getParentId()+ " Name : "+category2.getCategoryName());

                 List<DrmCategory> cate3 = categoryRepository.findByParentId(category2.getCategoryId());
                 cate3.forEach(category3 -> {
                     System.out.println(" Level3 -> Parent ID : "+category3.getParentId()+ " Name : "+category3.getCategoryName());

                     List<DrmCategory> cate4 = categoryRepository.findByParentId(category3.getCategoryId());
                        cate4.forEach(category4 -> {
                             System.out.println(" Level4 -> Parent ID : "+category4.getParentId()+ " Name : "+category4.getCategoryName());
                     });
                 });
              }
            );
          }
        );
因此,对于第一个级别的类别,我基于其parentId为0的类别进行获取,对于其余三个级别的类别,我基于其parentId对应于categoryId的类别进行获取。最后,我需要将所有类别的列表返回到域对象。所以我的抓取代码如下所示

categoryId, categoryName, parentId, active
 List<DrmCategory> cate1 = categoryRepository.findByParentId(0);
        cate1.forEach(category -> {
             System.out.println(" Level1 -> Parent ID : "+category.getParentId()+ " Name : "+category.getCategoryName());

             List<DrmCategory> cate2 = categoryRepository.findByParentId(category.getCategoryId());
             cate2.forEach(category2 ->{
                 System.out.println(" Level2 -> Parent ID : "+category2.getParentId()+ " Name : "+category2.getCategoryName());

                 List<DrmCategory> cate3 = categoryRepository.findByParentId(category2.getCategoryId());
                 cate3.forEach(category3 -> {
                     System.out.println(" Level3 -> Parent ID : "+category3.getParentId()+ " Name : "+category3.getCategoryName());

                     List<DrmCategory> cate4 = categoryRepository.findByParentId(category3.getCategoryId());
                        cate4.forEach(category4 -> {
                             System.out.println(" Level4 -> Parent ID : "+category4.getParentId()+ " Name : "+category4.getCategoryName());
                     });
                 });
              }
            );
          }
        );
List cate1=categoryRepository.findByParentId(0);
类别1.forEach(类别->{
System.out.println(“Level1->Parent ID:+category.getParentId()+”Name:+category.getCategoryName());
List cate2=categoryRepository.findByParentId(category.getCategoryId());
类别2.forEach(类别2->{
System.out.println(“Level2->Parent ID:+category2.getParentId()+”Name:+category2.getCategoryName());
List cate3=categoryRepository.findByParentId(category2.getCategoryId());
类别3.forEach(类别3->{
System.out.println(“Level3->Parent ID:+category3.getParentId()+”Name:+category3.getCategoryName());
List cate4=categoryRepository.findByParentId(category3.getCategoryId());
类别4.forEach(类别4->{
System.out.println(“Level4->Parent ID:+category4.getParentId()+”Name:+category4.getCategoryName());
});
});
}
);
}
);

我觉得上面的代码不是实现该功能的最佳方式。任何人都可以给出一些建议来增强嵌套for循环条件。或者,如果有人建议更好的设计(表设计和嵌套循环获取所有级别类别),我将不胜感激。

您可以尝试如下递归方法:

void recursiveMethod(int currentId, int depth) {

    List<DrmCategory> cate = categoryRepository.findByParentId(currentId);
    cate.forEach(category -> {
         System.out.println(" Level"+depth+" -> Parent ID : "+category.getParentId()+ " Name : "+category.getCategoryName());

        if (depth < 4) {
            recursiveMethod(category.getCategoryId(), depth + 1);
        }
    });
}
void recursiveMethod(int-currentId,int-depth){
List cate=categoryRepository.findByParentId(currentId);
类别forEach(类别->{
System.out.println(“级别”+深度+”->父ID:“+category.getParentId()+”名称:“+category.getCategoryName());
如果(深度<4){
递归方法(category.getCategoryId(),深度+1);
}
});
}
为了保持干净,您应该调用另一个方法来管理控件,具体取决于深度<代码>剂量测量(类别、深度)

要开始递归,调用
recursiveMethod(0,1)

请注意,这是盲编码,您可能会发现语法错误。

使用我的免费库:

EntryStream.ofTree(null,
(深度,cat)->深度>=4?空值:
categoryRepository.findByParentId(cat==null?0:cat.getCategoryId()).stream())
.nonnull值()
.mapKeyValue((深度,类别)->“级别”+深度+”->父ID:
+cat.getParentId()+“名称:”+cat.getCategoryName()
.forEach(System.out::println);

该方法能够以
(深度,元素)
对的形式生成树状结构的平坦深度优先流。我们从
null
元素开始作为树根(根据您的示例,似乎没有类别ID为0的特殊“root”元素),然后生成子节点流,直到深度小于4为止。我们使用将人工
null
元素从流中排除,然后使用格式化输出字符串。

是否只想记录?还是做点别的?如果是这样的话,那又怎样?将for each循环转换为forEach流并不总是一个好主意……我会创建一个递归方法,以其深度级别作为参数调用自己,但这只是我的想法。有些人在递归方面有困难。@GuillaumeF。你能提供你的输入来创建递归方法吗。我一定会帮助meI在您的实体中添加一个自映射,这意味着您的实体将具有getParent方法,该方法将急切地调用其父对象(我假设您没有太多类别),谢谢。将尝试您的建议如果该建议对您有效,请确保批准答案:)