在Java 8中,从一个类型的集合复制值到另一个类型的集合:

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

copy value from set of one type to set of another type in java 8

问题

我有两个集合对象 Set<Foo> fooSetSet<Bar> barSetFoo 是实体对象,Bar 是用于向 UI 发送数据的 DTO。我想要将所有的 fooSet 属性复制到 barSet

public class Foo {
    @Id
    @Column(name = "Category_Id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long categoryId;

    @OneToMany(mappedBy="productCategory")
    private Set<ProductEntity> products;
}

public class BarDto {

   private Long categoryId;

   private Set<ProductDto> products;

}

我正在尝试将实体转换为 DTO 对象,像这样:

public BarDto mapDomainToDto(Foo domain) {
    BarDto barDto = new BarDto();
    barDto.setCategoryId(domain.getCategoryId());
    // 尝试做类似这样的操作:
    barDto.setProducts(domain.getProducts());
}

在 Java 8 中有没有实现这个的方法呢?

英文:

I have two set object Set&lt;Foo&gt; fooSet and Set&lt;Bar&gt; barSet. Foo is the entity object and Bar is the DTO which i want to use for sending data to the UI. I want to copy all the properties from fooSet into barSet.

public class Foo {
    @Id
    @Column(name = &quot;Category_Id&quot;)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long categoryId;

    @OneToMany(mappedBy=&quot;productCategory&quot;)
    private Set&lt;ProductEntity&gt; products;
}

public class BarDto {

   private Long categoryId;

   private Set&lt;ProductDto&gt; products;

}

I am trying to convert Entity into DTO object like:

public BarDto mapDomainToDto(Foo domain) {

		BarDto barDto= new BarDto();
        barDto.setCategoryId(domain.getCategoryId());
       //trying to do something like:
	   barDto.setProducts(domain.getProducts());
}

Is there any way to achieve this in Java 8?

答案1

得分: 2

Java 8 本身并没有提供这样的映射功能,纯粹的 Java 代码只能手动调用 getter 和 setter 方法来进行对象属性的赋值,就像你已经在做的那样:

BarDto barDto = new BarDto();
barDto.setCategoryId(domain.getCategoryId());
barDto.setProducts(domain.getProducts());
// ...

这种方法在对象映射和参数数量较少时还不错。但对于更复杂的对象层次结构,我推荐使用 MapStruct。另外一个选择是 ModelMapper(依我看,MapStruct 更易于配置和使用,虽然它们的工作原理是一样的)。

@Mapper
public interface FooBarMapper {
 
    FooBarMapper INSTANCE = Mappers.getMapper(FooBarMapper.class);

    BarDto fooToBarDto(Foo domain);
}
BarDto barDto = FooBarMapper.INSTANCE.fooToBarDto(domain);

回到 Java 8,我猜你可能指的是 Stream API、Optional,或者与函数式编程范式相关的内容。同样,在 Java 8 或更新版本中都没有这样的内置功能。然而,使用上述库可以帮助你映射集合中的对象:

FooBarMapper mapper = FooBarMapper.INSTANCE;

List<Foo> fooList = ...;
List<BarDto> barDtoList = fooList.stream()
                                 .map(mapper::fooToBarDto)
                                 .collect(Collectors.toList());
英文:

Java 8 itself doesn't provide such mapping feature and with pure Java all the left is manual calls of getters and setters as you already do:

BarDto barDto = new BarDto();
barDto.setCategoryId(domain.getCategoryId());
barDto.setProducts(domain.getProducts());
...

This is not bad itself as long as the number of such object mappings and parameters is low. For more complex object hierarchies I recommend MapStruct. ModelMapper is its alternative (IMHO, MapStruct is the way easier to configure and use, though it does the job the same).

@Mapper
public interface FooBarMapper {
 
    FooBarMapper INSTANCE = Mappers.getMapper(FooBarMapper.class);

    BarDto fooToBarDto(Foo domain);
}
BarDto barDto = FooBarMapper.INSTANCE.fooToBarDto(domain);

Back to Java 8, I bet you refer to either Stream API, Optional or anything related to the functional paradigm. Again, there is not such feature in this or any newer verstion of Java. However, it might help you with mapping of collections of objects using the library above:

FooBarMapper mapper = FooBarMapper.INSTANCE;

List&lt;Foo&gt; fooList = ...
List&lt;BarDto&gt; barDtoList = fooList.stream()
                                 .map(mapper::fooToBarDto)
                                 .collect(Collectors.toList());

答案2

得分: 0

通常我们会编写模型映射器。或者您可以使用一些库,比如
http://modelmapper.org/getting-started/

英文:

Usualy we write model mapper. Or u can use some libraries like
http://modelmapper.org/getting-started/

huangapple
  • 本文由 发表于 2020年8月20日 14:54:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63499776.html
匿名

发表评论

匿名网友

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

确定