title: Multilevel map in Java
body: <p>What is the best way in Java to keep values ("o") in a tree structure like this:</p>
  
  <pre><code>                    obj1                 
                       /\
                      /  \
                     /    \
                obj2        obj3
                /\            /\
               /  \          /  \
              /    \        /    \
            obj4  obj5    obj6   obj7
            /\     /\     /\      /\
           /  \   /  \   /  \    /  \
          o8   oN...
  </code></pre>
  
  <p>It looks like a tree, but I don't need arbitrary depth. I rather need strong datatyping and predefined good looking methods for working with final structure.</p>
  
  <p>I need to be able to get some kind of list of values by keys - exactly like on my picture. In other words, structure should <strong>not</strong> become planar in any way.</p>
  
  <p>I need <code>.get(obj3)</code> to return <code>{obj6, obj7}, .get(obj1) - {obj2, obj3}</code>.</p>
  
  <p>For now I use Map for that, but inflating such map is ugly, because I need to check each level of the structure. Looks like that (<strong>data</strong> is the map):</p>
  
  <pre><code>if(data.get(somedouble) == null) {
      Map&lt;Integer, Data&gt; inm = new TreeMap&lt;&gt;();
      inm.put(someint, obj);
      Map&lt;Double, Map&lt;Integer, Data&gt;&gt; m = new TreeMap&lt;&gt;();
      m.put(somedouble2, inm);
      data.put(somedouble, m);
  }
  else {
      if(data.get(somedouble).get(somedouble2) == null) {
          Map&lt;Integer, Data&gt; inm = new TreeMap&lt;&gt;();
          inm.put(someint, obj);
          data.get(somedouble).put(somedouble2, inm);
      }
      else
          data.get(somedouble).get(somedouble2).put(someint, obj);
  }
  </code></pre>
  
  <p>Performance in not an issue, but code beauty is.</p>