title: Cast Object to List of Custom DTOs in java
body: <p>Here's the code:</p>
  
  <pre><code>Object obj = getUsers();
  
  List&lt;UserDTO&gt; userDTOList = new ArrayList&lt;UserDTO&gt;();
  
  userDTOList = (ArrayList&lt;UserDTO&gt;)obj;
  
  for (UserDTO userDTO : userDTOList) {
  
     do some stuff with userDTO   
  
  }
  </code></pre>
  
  <p>The signature of "getUsers()" is as below</p>
  
  <pre><code>List&lt;UserDTO&gt; getUsers();
  </code></pre>
  
  <p>getUsers returns a list of users and if I print the object(The variable obj here) I can see them.</p>
  
  <p>The obj is actually a json response as below</p>
  
  <blockquote>
    <p>[{firstName=A,lastName=B,emailAddress=A.B@email.com},{firstName=C,lastName=D,emailAddress=C.D@email.com},{firstName=E,lastName=F,emailAddress=E.F@email.com}]</p>
  </blockquote>
  
  <p>Now the problem I'm facing is:</p>
  
  <p>After the casting when I'm iterating over the casted userDTOList I see all the individual userDTOs carry the same data though actually they are different as I can confirm when I print the object. It's like the first user information is getting copied to others.</p>
  
  <p>As with the example above, the 3 userDTOs should carry individual user information, but I'm getting say firstName=A for the three DTOs. </p>
  
  <p>Any idea how to fix this or what's going wrong here?</p>
  
  <h1>getUsers/setUsers from another DTO</h1>
  
  <pre><code>private List&lt;UserDTO&gt; s$user;
  
  public List&lt;UserDTO&gt; getUsers() {
      return s$user;
  }
  
  public void sets$user(List&lt;UserDTO&gt; s$user) {
      this.s$user = s$user;
  }
  </code></pre>
  
  <h1>UserDTO</h1>
  
  <pre><code>    private String s$firstName;
  private String s$lastName;
  private String s$emailAddress;
  </code></pre>
  
  <p>and their getter/setters</p>
  
  <p>All the DTOs are getting set from json parsing.</p>