Converting flat structure to typical parent-child tree in Java -


for reason i'm drawing blank on this, maybe brain overloaded lately can't seem right. i'm trying convert structured list of unique values tree structure.

i have list of unique values set in apache multikey classes, wrapper number of objects make key. list looks this:

list<multikey> _data = new arraylist<multikey>() 

and data inside looks this:

multikey[abc] multikey[abc, 111] multikey[abc, 111, chf] multikey[abc, 111, chf, at000b049432] multikey[abc, 111, chf, ch0012814965] multikey[abc, 111, chf, ch0018550399] multikey[abc, 111, chf, ch0020626773] multikey[abc, 111, eur] multikey[abc, 111, eur, at0000a001x2] multikey[abc, 111, eur, at0000a0u3t4] multikey[abc, 111, usd] multikey[abc, 111, usd, ch0002497458] multikey[def] multikey[def, 222] multikey[def, 222, chf] multikey[def, 222, chf, at000b049432] multikey[def, 222, chf, ch0012814965] multikey[def, 222, eur] multikey[def, 222, eur, at0000a001x2] 

the class makes tree node classic parent-child tree node looks (additional methods excluded simplicity).

public class datatreenode {      private object              _data;     private datatreenode        _parent;     private list<datatreenode>  _children;      public datatreenode() {         super();     }      public datatreenode(object data) {         super();         _data = data;     }      public datatreenode getparent() {         return _parent;     }      public void setparent(datatreenode parent) {         _parent = parent;     }      public object getdata() {         return _data;     }      public void addchild(datatreenode child) {         if (!_children.contains(child)) {             _children.add(child);             child.setparent(this);         }     }      public list<datatreenode> getchildren() {         return _children;     }  } 

the idea loop through keys , (using sample data) build tree this:

abc   111     chf       at000b049432       ch0012814965       ch0018550399       ch0020626773     eur       at0000a001x2       at0000a0u3t4     usd       ch0002497458 def   222     chf       at000b049432       ch0012814965     eur       at0000a001x2 

here's runnable java sample builds sample data using multikey structure, missing convert-to-tree implementation.

import java.util.arraylist; import java.util.list;  import org.apache.commons.collections.keyvalue.multikey;  public class sample {      public static void main(string [] args) {          list<multikey> data = new arraylist<multikey>();          data.add(new multikey(new object [] { "abc" }));         data.add(new multikey(new object [] { "abc", "111" }));         data.add(new multikey(new object [] { "abc", "111", "chf" }));         data.add(new multikey(new object [] { "abc", "111", "chf", "at000b049432" }));         data.add(new multikey(new object [] { "abc", "111", "chf", "ch0012814965" }));         data.add(new multikey(new object [] { "abc", "111", "chf", "ch0018550399" }));         data.add(new multikey(new object [] { "abc", "111", "chf", "ch0020626773" }));         data.add(new multikey(new object [] { "abc", "111", "eur" }));         data.add(new multikey(new object [] { "abc", "111", "eur", "at0000a001x2" }));         data.add(new multikey(new object [] { "abc", "111", "eur", "at0000a0u3t4" }));         data.add(new multikey(new object [] { "abc", "111", "usd" }));         data.add(new multikey(new object [] { "abc", "111", "usd", "at0000a0u3t4" }));         data.add(new multikey(new object [] { "def" }));         data.add(new multikey(new object [] { "def", "222" }));         data.add(new multikey(new object [] { "def", "222", "chf" }));         data.add(new multikey(new object [] { "def", "222", "chf", "at000b049432" }));         data.add(new multikey(new object [] { "def", "222", "chf", "ch0012814965" }));         data.add(new multikey(new object [] { "def", "222", "eur" }));         data.add(new multikey(new object [] { "def", "222", "eur", "at0000a001x2" }));          datatreenode treefromdata = sample.gettreefor(data);         // ...     }      public static datatreenode gettreefor(list<multikey> data) {         // todo: this!         return null;     } } 

thanks in advance solving this!

if not homework, presumably free use data structures simplify things.

i suggest using map map e.g. "abc" node representing it. make building tree simpler.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -