5 Replies Latest reply: Jan 26, 2013 2:49 PM by TPD-Opitz RSS

    structure from a array of strings

    SRAVZ
      All,

      I have a list of string path to be passed dynamically, for example:

      {"foo/admin/rick", "foo/read/ben", "foo/admin/rock", "bar/erp/call", "foo/cust/profile"}

      How to create an ordred string tree path? Or Where can I find a kind of library that can resolve my problems?


      Sample Output
      foo
         admin
            rick
            rock
         read
            ben
         cust
            profile
      bar
          erp
            call
      Thanks
      Sravz
        • 1. Re: Tree structure from a collection of strings
          TPD-Opitz
          SRAVZ wrote:
          {"foo/admin/rick", "foo/read/ben", "foo/admin/rock", "bar/erp/call", "foo/cust/profile"}
          How to create an ordred string tree path?
          I'd sugest <tt>String.split()</tt> and Maps in Maps:
          String[] paths = {"foo/admin/rick", "foo/read/ben", "foo/admin/rock", "bar/erp/call", "foo/cust/profile"};
          Map<String,Map> tree = new HashMap<>();
          for (String path: paths) {
            String[] sections = path.split("/");
            addTreeNode(tree, sections);
            System.out.println(tree); // Display is not (yet) tree like you have to code that youself for console output
          }
          private void addTreeNode(Map<String,Map>tree, String[] sections){
            Map<String, Map> subTree = new HashMap<>();
            if(tree.containsKey(sections[0]){
              subTree= tree.get(sections[0]);
            } else {
              tree.put(sections[0],subTree);
            }
            if(1<sections.length){
              addTreeNode((Map<String,Map>)subTree,  Arrays.copyOfRange(sections, 1,sections.length-1)); // recursive call
            }
          }
          bye
          TPD
          • 2. Re: structure from a array of strings
            SRAVZ
            Thanks for yor reply. There is missing a child in the output
                       
             {foo={admin={}}}
            {foo={admin={}, read={}}}
            {foo={admin={}, read={}}}
            {foo={admin={}, read={}}, bar={erp={}}}
            {foo={admin={}, read={}, cust={}}, bar={erp={}}}
            Also when the input is changed, throws an java.lang.ArrayIndexOutOfBoundsException

            Here is the input used
             String[] paths = {"foo/admin/rick/1", "foo/read/ben", "foo/admin/rock", "bar/erp/call", "foo/cust/profile"};
            thanks
            sravz
            • 3. Re: Tree structure from a collection of strings
              TPD-Opitz
              I gave you a more or less working example.

              I'll help you to unsterstand it but I'll not do you work.

              So where did you have problems to understand my code and what did you do to analyse the failure?

              bye
              TPD
              • 4. Re: structure from a array of strings
                SRAVZ
                for the error here is the problem
                    addTreeNode((Map<String,Map>)subTree,  Arrays.copyOfRange(sections, 1,sections.length-1)); // recursive call
                I was unable to figure out why the child node was missing.

                Thanks
                Sravz
                • 5. Re: structure from a array of strings
                  TPD-Opitz
                  SRAVZ wrote:
                  addTreeNode((Map<String,Map>)subTree,  Arrays.copyOfRange(sections, 1,sections.length-1)); // recursive call
                  I was unable to figure out why the child node was missing.
                  I made an asumption on the parameters of <tt>Arrays.copyOfRange()</tt> which was wrong. Are you more carefully in reading the API than me?

                  bye
                  TPD