如何编写查找父母和孩子的递归方法 [英] How to write a recursive method for finding parent and child
问题描述
在这里,我想从数据库中获取详细信息,并使用递归方法将其设置到我的bean中.这样我就可以在angularUi树形对象中显示.如何编写递归方法以设置到我的bean中.
Here I want to get the details from the Database and set into my bean with a recursive method. So I can dispaly in angularUi tree formate. How can I write the recursive method to set into my beans.
我的数据库严格程度:-
我使用rowId分隔父母和孩子.您可以访问我的示例屏幕.
I am separating the parents and childs with rowId.you can access my sample screen.
例如:-父级的Rowid为1 这个1的孩子是1.1 而1.1的孩子是1.1.1,这样会延长,.
Eg:- Rowid is 1 for parent the child for this 1 is 1.1 and child for 1.1 is 1.1.1 like this it will prolong,,.
我将所有父母和孩子都保存在图片上方的一张桌子中.
I am saving all the parents and child in a single table which is above image.
每个对象(行)都有items[]
.如果有任何子代为父,则该子代将被添加到该items[]
数组中;如果该子代有任何子代,则该子代将被添加到该行的items[]
父代中.
there will be items[]
for each and every object(Row). If there is any child for parent then the child will be added in that items[]
array, if that child have any child then the child will be added in the parent of that row's items[]
... like this it will prolong.
例如:-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": []
}
]
}
我已经使用但是在检索时我遇到了问题.问题是检索时不会有任何父项和子项,因为数据将保存在同一表中.该关系仅是rowid.为此,我需要编写一种类似保存的递归方法,并且需要将子级添加到父级items[]
数组中.
But while retriving I am facing problems. The problem is while retrieving there will not be any parent and childs as the data will be saved in same table.The relationship is only rowid. For This I need to write a recursive method like saving, and need to add the childs to parent items[]
array.
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;
}
在这里,我尝试检索所有详细信息,并尝试将子级添加到父级.但这应该是一种递归方法
Here I try to retrive all the details and trying to add the childs to parent.But it should make a recursive method
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
}
}
}
推荐答案
我建议不要采取递归的方式,而是将所有元素都存储在HashMap中
Instead of recursive I would suggest to go an extra step and store all elements in a 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可能是这样的:
getParentId could be something like this:
private String getParentId(String id) {
int lastDot = id.lastIndexOf(".");
if (lastDot == -1) {
return null;
}
return id.substring(0, lastDot);
}
如果可以保证componentList将从父级到子级遍历整个列表,则可以将两个for循环组合在一起.
If you can guarantee that componentList will go through the list from parents to children, you can combine both for-loops.
这篇关于如何编写查找父母和孩子的递归方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!