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


it's little bit difficult explain. in 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 create function fetch objects containing object in it's hierarchy "parent" field. example if "test1mem1obj1", should give me values of "{test1mem1, test1}" , if "test2mem1", should give me values of "{test2}". fetches parent of parent of parent , on. sorry explanation because of language barrier. hope can me out here. thank you!

i have temporary dirty solution , can see why 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 can have getparent() return someclass instead of 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 parent named parentname         }     }     return false; // reached parentless parent , never found 1 matching parentname } 

maybe that's not possible, though. if can put things in map instead, so:

map<string, someclass> map = new hashmap<>(); // map parent name 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 loop on 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 want populate list of parents given element, call function 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); } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -