【问题标题】:How to write a recursive method for finding parent and child如何编写查找父子的递归方法
【发布时间】:2018-01-19 21:31:11
【问题描述】:

This is to save the details with recursive.

在这里,我想从数据库中获取详细信息并使用递归方法设置到我的 bean 中。所以我可以在 angularUi 树格式中显示。如何编写递归方法来设置到我的 bean 中。

我的数据库严格:-

我正在使用 rowId 将父母和孩子分开。您可以访问我的示例screen

例如:- Rowid 为 1 表示父级 这个 1 的孩子是 1.1 1.1的孩子是1.1.1这样它会延长,,。

我将所有父母和孩子都保存在图片上方的单个表格中。

每个对象(行)都会有items[]。如果父级有任何子级,则该子级将添加到该items[] 数组中,如果该子级有任何子级,则该子级将添加到该行的items[] 的父级中...这样它会延长。

例如:- JSON 对象是:-

{
    "id": 1,
    "rowId": "1",
    "items": [
      {
        "id": 10,
        "rowId": "1.1",
        "items": [
          {
            "id": 100,
            "rowId": "1.1.1",
            "items": [
              {
                "id": 1000,
                "rowId": "1.1.1.1",
                "items": []
              }
            ]
          }
        ]
      },
      {
        "id": 11,
        "rowId": "1.2",
        "items": []
      }
    ]
  }

我已经使用this answer.保存了这些数据

但是在检索时我遇到了问题。问题是在检索时不会有任何父子节点,因为数据将保存在同一个表中。关系只有rowid。为此,我需要编写一个递归方法,例如保存,并且需要将子元素添加到父 items[] 数组中。

public class AdminComponentBean{

    List<MultiAdminComponent> componentListbean;
}

MultiAdminComponent.java:-

public class MultiAdminComponent {

    private String componentName;
    private String componentIdentification;
    private String componentType;
    private String componentState;
    private String componentUrl;
    private String rowId;
    private List<MultiAdminComponent> items;
}

在这里我尝试检索所有细节并尝试将孩子添加到父级。但它应该是一个递归方法

List<MultiAdminComponent> componentList=BaseDAO.getAdminComponentDAOObject().getComponentDetails();
   if(null != componentList) {
       for(MultiAdminComponent itemsList : componentList){
           if(itemsList.getRowId().length().equals() "1"){//here parent row will come
               //by considering rowid I need to find the child of the rowId
               //child of 1 is 1.1
               //if 1.1 is child of 1 then I need to add that 1.1 object to `items[]` array of 1
               //like this it should work recursve
            }
        }
    }

【问题讨论】:

  • 如果你有一个父 id(假设这就是“rowId”的意思)应该相当简单(更高的性能可能会使它更复杂,所以我们现在坚持简单):1)获取所有没有父级的元素 2) 直到您不再获取任何数据,执行以下操作:a) 获取 rowId 与上次加载的元素的 id 之一匹配的所有元素 b) 将加载的元素附加到它们的父级使用 rowId 进行映射。当你没有得到更多元素时,你就完成了。
  • rowid 对于每一行都是唯一的
  • @Thomas 你能说清楚一些吗
  • 实际上,看看 Absurd-Mind 的答案,它似乎利用了“rowId”基本上包含所有父母各自级别的索引(因此rowId = parent.rowId + indexInParent)这一事实。因此,您可以在一个查询中加载部分树(例如,... where rowId in ('1','1.1') or rowId like '1.1.%' 以获取元素 1、1.1 和 1.1 的所有子元素),并且正如 Absurd-Mind 建议的那样,使用地图将加载的元素相互连接。

标签: javascript java angularjs json spring


【解决方案1】:

我建议不要使用递归,而是将所有元素存储在 HashMap 中

// a map containing all elements searchable by the rowId
HashMap<String, MultiAdminComponent> idToItem = new HashMap<>();
// a set containing all elements that don't have a parent (id: 1, 2, 3, etc.)
Set<MultiAdminComponent> rootItems = new HashSet<>();

for (MultiAdminComponent item : componentList) {
    // build the id->item map
    idToItem.put(item.getRowId(), item);
}

for (MultiAdminComponent item : componentList) {
    String parentId = getParentId(item.getRowId());
    if (parentId == null) {
        // this item has no parent -> it is a root item
        rootItems.add(item);
    } else {
        // This item has a parent -> look the parent up
        MultiAdminComponent parent = idToItem.get(parentId);
        parent.getItems().add(item);
    }
}

// rootItems now contains all MultiAdminComponents which do not have a parent, with the correct hierarchy for all items

getParentId 可能是这样的:

private String getParentId(String id) {
    int lastDot = id.lastIndexOf(".");
    if (lastDot == -1) {
        return null;
    }
    return id.substring(0, lastDot);
}

如果你能保证 componentList 会遍历从父到子的列表,你可以结合两个 for 循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    相关资源
    最近更新 更多