How to write a recursive method to search for parent and child

This is needed to save details with recursive.

Here I want to get data from a database and set a recursive method to my bean. So I can figure out the angularUi tree formate. How to write a recursive method to install in my beans.

My strict DB: - enter image description here

I separate parents and children from rowId. You can access my sample.

For example: - Rowid - 1 for the parent child for this 1 is 1.1 and the child for 1.1 is 1.1.1, as this will extend.

I keep all the parents and the child in one table, which is above the image.

for each object (row) will be items[]. If there is any child element for the parent, then the child will be added to this array items[], if this child has any child, then the child will be added to the parent element of this row items[]... for example, it will be extended.

For example: - JSON object: -

{
    "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": []
      }
    ]
  }

I saved this data using this answer.

But when recovering, I ran into problems. The problem is that when retrieving, there will be no parent and child elements, since the data will be stored in one table. The relationship is just rowid. To do this, I need to write a recursive method, such as save, and add children to the parent array 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;
}

parent.But

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
            }
        }
    }
+4
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-loops.

+3

Source: https://habr.com/ru/post/1683522/


All Articles