title: How to deserialize nested Firebase database in Java with Map<String, Obect> or List <object> class?
body: <p>Here is my simple primary Firebase data.</p>
  
  <pre><code>{
    "elephant" : {
      "color" : "gray",
      "lifespan" : 48,
      "status" : "endangered"
    }
  }
  </code></pre>
  
  <p>In my android project I made this class to represent this data</p>
  
  <pre><code>public class elephant{
      private String color;
      private int lifespan;
      private String status;
  
      // Constructors
      public elephant(){
      }
  
      public elephant(String color, int lifespan, String status) { . . . }
  
      // Getters and Setters
      public String getColor() { return color; }
  
      public void setColor(String color) { this.color = color; }
  
      public int getLifespan() { return lifespan; }
  
      public void setLifespan(int lifespan) { this.lifespan = lifespan; }
  
      public String getStatus() { return status; }
  
      public void setStatus(String status) { this.status = status; }
  }
  </code></pre>
  
  <p>That way I can call getValue(elephant.class) method to retrieve this data.</p>
  
  <pre><code>Elephant ele = datasnapshot.getValue(elephant.class);
  </code></pre>
  
  <p>It works.</p>
  
  <p>Now I'm adding one nested node in my project.</p>
  
  <pre><code>{
    "elephant" : {
      "color" : "gray",
      "lifespan" : 48,
      "status" : "endangered",
      "dimension" : {
        "height" : 270,
        "length" : 607
      }
    }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/sWsn9.png" alt="database Image"></p>
  
  <p>I've created a new class called Dimension.java in my project:</p>
  
  <pre><code>public class Dimension {
      private int length, height;
  
      // Constructors
      public Dimension() { }
  
      public Dimension(int length, int height) { . . . }
  
      // Public getters and setters
      public int getLength() {return length;}
  
      public void setLength(int length) {this.length = length;}
  
      public int getHeight() {return height;}
  
      public void setHeight(int height) {this.height = height;}
  }
  </code></pre>
  
  <p>And I modified my elephant.java to include this variable/object:</p>
  
  <pre><code>private Dimension dimension;
  </code></pre>
  
  <p>and proper constructor, getters and setters for it.</p>
  
  <p>and my previous method still works. It returns the proper java representation for the whole datasnapshot.</p>
  
  <pre><code>Elephant ele = datasnapshot.getValue(elephant.class);
  </code></pre>
  
  <p>and, If I get a datasnapshot at DatabaseRef.child('dimension') I can pass the dimension.class to getValue() method to get proper data:</p>
  
  <pre><code>Dimension data = datasnapshot.getValue(dimension.class);
  </code></pre>
  
  <p>Thus these multipurpose classes are pretty convenient to me.</p>
  
  <p>Now, I know, Firebase data is supposed to be normalized/flattened. But for experiments sake let's say I've added some data with pushID. </p>
  
  <pre><code>{
    "elephant" : {
      "color" : "gray",
      "status" : "endangered",
      "lifespan" : 48,
      "dimension" : {
        "height" : 270,
        "length" : 607
      },
      "population" : {
        "-KTcpw_SsMb55qLc6Xme" : {
          "count" : 210,
          "location" : "Bangladesh"
        },
        "-KTcq6YeB3kX3h9F9xPU" : {
          "count" : 428,
          "location" : "Bhutan"
        },
        "-KTcqDySLBAFtCuUc3R4" : {...},
        "-KTcqMyy4GULKo6FqPEM" : {...},
        "-KTcqWwtli672ROpry8_" : {...},
        "-KTcqhnBb6dTN1mA1otW" : {...}
      }
    }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/I5WU9.png" alt="Database Screenshot"></p>
  
  <p>Now, If I want to make previous code work for this case:</p>
  
  <pre><code>Elephant ele = datasnapshot.getValue(elephant.class);
  </code></pre>
  
  <p>How do I write my new class for the 'population' node.</p>
  
  <p>In Firebase reference it is said that it returns a <code>Map&lt;String, Object&gt;</code> or <code>List&lt;Object&gt;</code> java equivalent. But being a beginner in Java I'm stuck with this problem in past 4 days.</p>
  
  <p>I can iterate through the <code>dataSnapshot.child('population')</code> and add them in a List of separate '<code>population</code>' object. But I've wondered if it can be achieved in this way. Please help me with the code or link me to a example where it was done in one line and automatically. It'll be such a relief.</p>