如何根据父子关系获取项目中嵌套(层次结构)的所有项目的列表?
How can I get the list of all the items nested(hierarchy) within the item based on parent-child relationship?
解释起来有点困难。在此示例代码中:
public class SomeClass
{
private String id;
private String parent;
public SomeClass(String id, String parent)
{
this.id = id;
this.parent = parent;
}
public String getParent()
{
return parent;
}
}
List<SomeClass> someList = new ArrayList();
someList.add(new SomeClass("Test1", "none"));
someList.add(new SomeClass("Test2", "none"));
someList.add(new SomeClass("Test1Mem1", "Test1"));
someList.add(new SomeClass("Test2Mem1", "Test2"));
someList.add(new SomeClass("Test1Mem1Obj1", "Test1Mem1"));
我想创建一个函数,该函数将获取所有对象,该对象在其层次结构中包含具有 "parent" 字段的对象。因此,例如,如果我查找 "Test1Mem1Obj1",它应该给我“{Test1Mem1, Test1}”的值,如果我查找 "Test2Mem1",它应该给我“{Test2”的值}”。基本上获取父对象的父对象的父对象等等。由于语言障碍,我对此解释感到抱歉。我希望有人能在这里帮助我。谢谢!
我有一个临时的脏解决方案,你会明白为什么这不好。
if(someObj.getParent() != null)
{
result.add(someObj.getParent());
if(someObj.getParent().getParent() != null)
{
result.add(someObj.getParent().getParent());
if(someObj.getParent().getParent().getParent() != null)
{
result.add(someObj.getParent().getParent().getParent());
}
}
}
如果你可以 getParent()
return 一个 SomeClass
而不是 String
,那就很简单了:
public boolean isDescendantOf(String parentName) { // part of SomeClass
SomeClass parent = this.parent;
while (!parent.id.equals("none")) { // or null check
if (parent.id.equals(parentName)) {
return true; // found a parent named parentName
}
}
return false; // eventually reached a parentless parent and never found one matching parentName
}
不过,也许这是不可能的。如果你可以把东西放在地图上,像这样:
Map<String, SomeClass> map = new HashMap<>(); // map from parent name to SomeClass
map.put("Test1", new SomeClass("Test1", "none"));
map.put("Test2", new SomeClass("Test2", "none"));
map.put("Test1Mem1", new SomeClass("Test1Mem1", "Test1"));
map.put("Test2Mem1", new SomeClass("Test2Mem1", "Test2"));
map.put("Test1Mem1Obj1", new SomeClass("Test1Mem1Obj1", "Test1Mem1"));
然后你可以像这样循环遍历它,使用递归:
public boolean isDescendentOf(SomeClass child, String parentName) {
SomeClass parent = map.get(child.parent);
if (parent == null) {
throw new RuntimeException("Warning: parent doesn't exist!");
}
if (parent.id.equals(parentName)) {
return true;
} else {
return isDescendentOf(parent, parentName);
}
}
如果要填充给定元素的所有父元素的列表,请调用如下函数:
public static void PopulateParents(List<String> parents, Map<String, ClassTest> nodes, ClassTest child) {
if (child.parent.equals("none")) {
return;
}
ClassTest parent = nodes.get(child.parent);
if (parent == null) {
throw new RuntimeException("No parent exists called " + child.parent);
}
parents.add(parent.id);
PopulateParents(parents, nodes, parent);
}
解释起来有点困难。在此示例代码中:
public class SomeClass
{
private String id;
private String parent;
public SomeClass(String id, String parent)
{
this.id = id;
this.parent = parent;
}
public String getParent()
{
return parent;
}
}
List<SomeClass> someList = new ArrayList();
someList.add(new SomeClass("Test1", "none"));
someList.add(new SomeClass("Test2", "none"));
someList.add(new SomeClass("Test1Mem1", "Test1"));
someList.add(new SomeClass("Test2Mem1", "Test2"));
someList.add(new SomeClass("Test1Mem1Obj1", "Test1Mem1"));
我想创建一个函数,该函数将获取所有对象,该对象在其层次结构中包含具有 "parent" 字段的对象。因此,例如,如果我查找 "Test1Mem1Obj1",它应该给我“{Test1Mem1, Test1}”的值,如果我查找 "Test2Mem1",它应该给我“{Test2”的值}”。基本上获取父对象的父对象的父对象等等。由于语言障碍,我对此解释感到抱歉。我希望有人能在这里帮助我。谢谢!
我有一个临时的脏解决方案,你会明白为什么这不好。
if(someObj.getParent() != null)
{
result.add(someObj.getParent());
if(someObj.getParent().getParent() != null)
{
result.add(someObj.getParent().getParent());
if(someObj.getParent().getParent().getParent() != null)
{
result.add(someObj.getParent().getParent().getParent());
}
}
}
如果你可以 getParent()
return 一个 SomeClass
而不是 String
,那就很简单了:
public boolean isDescendantOf(String parentName) { // part of SomeClass
SomeClass parent = this.parent;
while (!parent.id.equals("none")) { // or null check
if (parent.id.equals(parentName)) {
return true; // found a parent named parentName
}
}
return false; // eventually reached a parentless parent and never found one matching parentName
}
不过,也许这是不可能的。如果你可以把东西放在地图上,像这样:
Map<String, SomeClass> map = new HashMap<>(); // map from parent name to SomeClass
map.put("Test1", new SomeClass("Test1", "none"));
map.put("Test2", new SomeClass("Test2", "none"));
map.put("Test1Mem1", new SomeClass("Test1Mem1", "Test1"));
map.put("Test2Mem1", new SomeClass("Test2Mem1", "Test2"));
map.put("Test1Mem1Obj1", new SomeClass("Test1Mem1Obj1", "Test1Mem1"));
然后你可以像这样循环遍历它,使用递归:
public boolean isDescendentOf(SomeClass child, String parentName) {
SomeClass parent = map.get(child.parent);
if (parent == null) {
throw new RuntimeException("Warning: parent doesn't exist!");
}
if (parent.id.equals(parentName)) {
return true;
} else {
return isDescendentOf(parent, parentName);
}
}
如果要填充给定元素的所有父元素的列表,请调用如下函数:
public static void PopulateParents(List<String> parents, Map<String, ClassTest> nodes, ClassTest child) {
if (child.parent.equals("none")) {
return;
}
ClassTest parent = nodes.get(child.parent);
if (parent == null) {
throw new RuntimeException("No parent exists called " + child.parent);
}
parents.add(parent.id);
PopulateParents(parents, nodes, parent);
}