Java将Map和Set转换到另一个Set中(使用Model Mapper或其他任何方法)

huangapple go评论88阅读模式
英文:

Java map Set to another Set (Using Model Mapper or Any other way)

问题

在我目前正在工作的项目中,有一个名为 Project 的类,

public class Project {
  private Integer projectId;
  private String projectName;
  // 获取器和设置器(getters & setters)
}

这个项目在 User 类中被用作 Set

public class User {
  private Integer userId;
  private Set<Project> projects;
  // 获取器和设置器(getters & setters)
}

我需要将这个 User 类映射到 UserDto。UserDto 如下所示。

public class UserDto {
  private Integer userId;
  private Set<ProjectDto> projectDtos;
  // 获取器和设置器(getters & setters)
}

ProjectDto 也和 Project 类相同。

public class ProjectDto {
  private Integer projectId;
  private String projectName;
  // 获取器和设置器(getters & setters)
}

为了将 User 映射到 UserDto,我尝试了下面的代码。但是它报错了。

private UserDto map(User user) {
  UserDto userDto = new UserDto();
  userDto.setUserId(user.getUserId());
  Set<ProjectDto> projectDtos = new HashSet<>();
  Type setType = new TypeToken<HashSet<ProjectDto>>(){}.getType();
  projectDtos = modelMapper.map(user.getProjects(), setType);
  userDto.setProjectDtos(projectDtos);
  return userDto;
}

返回的错误如下:

> 无法将 org.hibernate.collection.internal.PersistentSet 转换为 java.util.Set

如果你能对此提供支持,将会非常棒。也欢迎不使用 ModelMapper 的答案。提前感谢!

英文:

In the project currently I'm working on, has a Project class,

public class project {
  private Integer projectId;
  private String projectName;
  //getters &amp; Setters
}

This project is using as a Set in User class

public class User {
  private Integer userId;
  private Set&lt;Project&gt; projects;
  //getters &amp; Setters
}

I need to map this User class to UserDto. UserDto is like below.

public class UserDto {
  private Integer userId;
  private Set&lt;ProjectDto&gt; projectDtos;
  //getters &amp; Setters
}

The ProjectDto is also same as Project class.

public class projectDto {
  private Integer projectId;
  private String projectName;
  //getters &amp; Setters
}

To map User and UseDto, I tried below code. But it's giving an error.

private UserDto map(User user) {
  UserDto userDto = new UserDto();
  userDto.setUserId(user.getUserId());
  Set&lt;ProjectDto&gt; projectDto= new HashSet&lt;&gt;();
  Type setType = new TypeToken&lt;HashSet&lt;ProjectDto&gt;&gt;(){}.getType();
  projectDto = modelMapper.map(user.getProjects(), setType);
  userDto.setProjectDto(projectDto);
  return userDto;
}

Following error is returned:

> failed to convert org.hibernate.collection.internal.PersistentSet to
> java.util.Set

It'd be great if you could support me for this. Answers without using ModelMapper are also welcome.
Thanks in advance!

答案1

得分: 3

首先在 ProjectDto 类中添加一个构造函数:

public class ProjectDto {

  private Integer projectId;
  private String projectName;

   public ProjectDto(Integer projectId, String projectName) {
      this.projectId = projectId;
      this.projectName = projectName;
   }
   // 获取器和设置器
}

然后使用 Java 8 的流式 API,可以像这样完成:

private UserDto map(User user) {
    UserDto userDto = new UserDto();
    userDto.setUserId(user.getUserId());
    userDto.setProjects(user.getProjects().stream()
            .map(project -> new ProjectDto(project.getProjectId(), project.getProjectName()))
            .collect(Collectors.toSet()));
    return userDto;
}
英文:

first add a constructor to ProjectDto class:

public class ProjectDto {

  private Integer projectId;
  private String projectName;

   public ProjectDto(Integer projectId, String projectName) {
      this.projectId = projectId;
      this.projectName = projectName;
   }
   //getters &amp; Setters
}

then with java 8 stream API, it can be done like this:

private UserDto map(User user) {
    UserDto userDto = new UserDto();
    userDto.setUserId(user.getUserId());
    userDto.setProjects(user.getProjects().stream()
            .map(project -&gt; new ProjectDto(project.getProjectId(), project.getProjectName()))
            .collect(Collectors.toSet()));
    return userDto;
}

huangapple
  • 本文由 发表于 2020年9月11日 15:24:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63842522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定