【问题标题】:need suggestions for enhancing nested for loop in Java需要建议以增强 Java 中的嵌套 for 循环
【发布时间】:2016-03-22 20:59:49
【问题描述】:

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

categoryId, categoryName, parentId, active

因此,对于第一级类别,我根据其 parentId 为 0 进行提取,而对于其余三个类别级别,我根据其 parentId 对应于 categoryId 进行提取。最后,我需要将所有类别的列表返回到域对象。所以我的抓取代码如下,

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

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

【问题讨论】:

  • 您只想登录吗?还是做点别的?如果是这样呢?
  • 将 for-each 循环转换为 forEach 流并不总是一个好主意...
  • 我会创建一个递归方法,以它的深度级别作为参数调用自己,但这只是我。有些人在递归方面遇到了麻烦。
  • @GuillaumeF。你能提供你的输入来创建递归方法吗?我一定会帮助我的
  • 我会在您的实体中添加一个自映射,这意味着您的实体将具有 getParent 方法,该方法将热切地调用其父级(我假设您没有太多类别)

标签: java for-loop java-8 spring-data spring-data-jpa


【解决方案1】:

你可以试试这样的递归方法:

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

为了干净,您应该根据深度调用另一种方法来管理您的控件。 doSomething(category, depth);

要开始递归,请致电recursiveMethod(0,1);

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

【讨论】:

  • 谢谢。会尝试你的建议
  • 如果该建议对您有用,请确保批准答案 :)
【解决方案2】:

使用我的免费 StreamEx 库:

EntryStream.<DrmCategory>ofTree(null, 
    (depth, cat) -> depth >= 4 ? null : 
        categoryRepository.findByParentId(cat == null ? 0 : cat.getCategoryId()).stream())
    .nonNullValues()
    .mapKeyValue((depth, cat) -> " Level"+depth+" -> Parent ID : "
            + cat.getParentId() + " Name : " + cat.getCategoryName())
    .forEach(System.out::println);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2016-03-23
    相关资源
    最近更新 更多