猿问

如何在java中将数据从一个对象列表复制到另一个对象?

我有以下 2 个对象Team和Group. 我在每个类中都有标准的 getter setter 和 toString 方法,并且不允许修改它们。


public class Team { 

  private List<Team> teams;

  private List<TeamMember> members;

  private String teamId;    

}


public class Group {    

  private List<GroupMember> groupMember;

  private List<Group> groups;

  private String groupId;

}

团队可以有一个List<Team>列表类型作为属性,其中List<Group>可以有一个List<Group>作为属性。


团队的示例列表如下所示:

我想创建反映相同结构的组列表TeamList。


这是我到目前为止所得到的。


@Service

public class GroupService {


@Autowired

TeamService teamService;


public List<Group> createGroupList(){


    List<Group> groups = Collections.emptyList();


    List<Team> teams = teamService.createTeamList();


    if (teams != null && !teams.isEmpty()) {


        groups = teams.stream().map(t -> {


            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<Group> getSubgroups(List<Team> teams, String parentGroupName) {


    Optional<Team> parentTeam = teams.stream()

            .filter(t -> t.getTeamId().equalsIgnoreCase(parentGroupName)).findFirst();


    if(parentTeam.isPresent()){


        List<Team> subTeams = new ArrayList<>();

        List<Group> lstOfGroups = new ArrayList<>();


        System.out.println("parentname " + parentTeam.get().getTeamId());



        if(parentTeam.get().getTeams() != null){

            parentTeam.get().getTeams().stream().forEach(r -> {

                subTeams.add(r);

            });

        }


        subTeams.stream().forEach(st -> {

            Group gr = new Group();

            gr.setGroupId(st.getTeamId());

            lstOfGroups.add(gr);    

        });


        return lstOfGroups;


    }

    return null;            


 }


}

我的想法是修改 getSubgroups 方法来为每个给定的路径正确设置子组。(例如:getSubgroubs 可以返回 team2,并将其所有子组设置为 team7)我知道我必须使用递归,但我正在努力寻找解决方案。我怎样才能做到这一点?


慕标琳琳
浏览 664回答 2
2回答

隔江千里

您可以只创建一个方法来将一个方法复制到另一个方法中并递归调用它:public Group toGroup(Team team) {&nbsp; &nbsp; Group result = new Group(team.teamId());&nbsp; &nbsp; // this is missing in your sample code&nbsp; &nbsp; result.setGroupMembers(transform(team.getTeamMembers());&nbsp; &nbsp; List<Group> subGroups = team.getTeams().stream()&nbsp; &nbsp; &nbsp; &nbsp; .map(this::toGroup) // recursive call to this method&nbsp; &nbsp; &nbsp; &nbsp; .collect(toList());&nbsp; &nbsp; result.setSubgroups(subGroups);&nbsp; &nbsp; return result;}所以你可以做List<Group> groups = teamService.getTeams()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// you should return an empty list if no elements are present&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.stream()&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(this::toGroup) // initial call&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(toList());您可能还想查看可以自动生成简单映射器的mapstruct。

梦里花落0921

为了让您了解这在 mapstruct 中的外观:@Mapper(componentModel="spring")interface TeamGroupMapper {&nbsp; &nbsp; @Mappings({&nbsp; &nbsp; &nbsp; &nbsp; @Mapping(source="teamId", target="groupId"),&nbsp; &nbsp; &nbsp; &nbsp; @Mapping(source="teams", target="groups"),&nbsp; &nbsp; &nbsp; &nbsp; @Mapping(source="teamMembers", target="groupMembers")&nbsp; &nbsp; })&nbsp; &nbsp; Group toGroup(Team team);&nbsp; &nbsp; List<Group> toGroups(List<Team> teams);&nbsp; &nbsp; GroupMember toGroupMember(TeamMember teamMember);}将生成实际代码。如果类具有同名的属性(例如,如果id为Team和调用了 id Group?),@Mapping则不需要对其进行注释。然后,您可以将@Autowire其作为组件使用。@Componentclass YourGroupService implements GroupService {&nbsp; &nbsp; @Autowired TeamGroupMapper mapper;&nbsp; &nbsp; @Autowired TeamService teamService;&nbsp; &nbsp; public List<Group> getGroups() {&nbsp; &nbsp; &nbsp; &nbsp; return mapper.toGroups(teamService.getTeams());&nbsp; &nbsp; }}我确信这段代码实际上不会工作,但它应该让您了解 mapstruct 的作用。我真的很喜欢它避免样板映射代码。
随时随地看视频慕课网APP

相关分类

Java
我要回答