【问题标题】:DozerMapper maps by default, Set to HashSet which destroys the ordering of elementsDozerMapper 默认映射,设置为 HashSet 破坏元素的顺序
【发布时间】:2019-07-25 17:47:51
【问题描述】:

我有 2 个 DTOS,即 OutputResponse 和 OutputDTO。类的格式如下:

class OutputDTO{

    private LinkedHashSet<String> items;
}

class OutputResponse{

    private Set<String> items;
}

当我尝试使用 DozerMapper 映射这 2 个对象时,它会将目标映射到 HashSet 而不是 LinkedHashSet。

class X{

    DozerBeanMapper mapper;

    mapper.map(OutputDTO.class, OutputResponse.class);

}

在上述场景中,map() 方法将 LinkedHashSet 映射到 HashSet,这破坏了输出响应的顺序。对此有何建议?

【问题讨论】:

    标签: java dozer


    【解决方案1】:

    如果您希望 OutputResponse 中的 Set of items 有某种顺序,您将需要使用不同的 Collection 实现。 Set 根据定义是无序的。 List 是有序的,LinkedHashSet 也是如此。通过将项目定义为一个集合,您已经定义了没有特定的顺序,并且 Dozer 通过使用 HashSet 来遵守该规范,它也是无序的。

    【讨论】:

      猜你喜欢
      • 2014-11-27
      • 2011-09-04
      • 2010-11-19
      • 1970-01-01
      • 2015-06-22
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      相关资源
      最近更新 更多