简体   繁体   中英

How can I get the list of all the items nested(hierarchy) within the item based on parent-child relationship?

It's a little bit difficult to explain. In this example code:

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

I want to create a function that will fetch all the objects that is containing an object in it's hierarchy with the "parent" field. So for example if I look up for "Test1Mem1Obj1", it should give me the values of "{Test1Mem1, Test1}" and if I look up for "Test2Mem1", it should give me the values of "{Test2}". Basically fetches the parent of the parent of the parent and so on. I am sorry for this explanation because of the language barrier. I hope someone can help me out here. Thank you!

I have a temporary dirty solution and you can see why this is not good.

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

If you can have getParent() return a SomeClass instead of a String , it's pretty easy:

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
}

Maybe that's not possible, though. If you can put things in a map instead, like so:

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

Then you could loop over it like this, using recursion:

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

If you want to populate a list of all the parents for a given element, call a function like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM