title: How to copy data from one list of objects to another in java?
body: <p>I have the following 2 objects <code>Team</code> and <code>Group</code>. I have standard getters setters and toString methods in each of these classes and not allowed to modify them.</p>
  
  <pre><code>public class Team { 
    private List&lt;Team&gt; teams;
    private List&lt;TeamMember&gt; members;
    private String teamId;    
  }
  
  public class Group {    
    private List&lt;GroupMember&gt; groupMember;
    private List&lt;Group&gt; groups;
    private String groupId;
  }
  </code></pre>
  
  <p>Team can have a <code>List&lt;Team&gt;</code> type of a list as an attribute where <code>List&lt;Group&gt;</code> can have a <code>List&lt;Group&gt;</code> as an attribute.</p>
  
  <p>example list of Teams look like follows:
  <a href="https://i.stack.imgur.com/Bsi9R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Bsi9R.png" alt="enter image description here"></a></p>
  
  <p>I want to create the list of Groups which reflects the same structure of <code>TeamList</code>.</p>
  
  <p>This is what I have got so far.</p>
  
  <pre><code>@Service
  public class GroupService {
  
  @Autowired
  TeamService teamService;
  
  public List&lt;Group&gt; createGroupList(){
  
      List&lt;Group&gt; groups = Collections.emptyList();
  
      List&lt;Team&gt; teams = teamService.createTeamList();
  
      if (teams != null &amp;&amp; !teams.isEmpty()) {
  
          groups = teams.stream().map(t -&gt; {
  
              Group group = new Group();
              group.setGroupId(t.getTeamId());
  
              //this is to be modified
              group.setGroups(getSubgroups(teams, group.getGroupId()));
  
              return group;
  
          }).collect(Collectors.toList());
  
      }
  
      return groups;
  }
  
  
  
  private List&lt;Group&gt; getSubgroups(List&lt;Team&gt; teams, String parentGroupName) {
  
      Optional&lt;Team&gt; parentTeam = teams.stream()
              .filter(t -&gt; t.getTeamId().equalsIgnoreCase(parentGroupName)).findFirst();
  
      if(parentTeam.isPresent()){
  
          List&lt;Team&gt; subTeams = new ArrayList&lt;&gt;();
          List&lt;Group&gt; lstOfGroups = new ArrayList&lt;&gt;();
  
          System.out.println("parentname " + parentTeam.get().getTeamId());
  
  
          if(parentTeam.get().getTeams() != null){
              parentTeam.get().getTeams().stream().forEach(r -&gt; {
                  subTeams.add(r);
              });
          }
  
          subTeams.stream().forEach(st -&gt; {
              Group gr = new Group();
              gr.setGroupId(st.getTeamId());
              lstOfGroups.add(gr);    
          });
  
          return lstOfGroups;
  
      }
      return null;            
  
   }
  
  }
  </code></pre>
  
  <p>My idea is to modify the getSubgroups method to set the subgroups correctly for each given path.(eg: getSubgroubs can return team2 with all it's subgroups set until team7) I know I have to use recursion but I am struggling to find the solution. How can I achieve this? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have updated my code and now I can access the first level of children and not the other levels yet</p>