简体   繁体   English

默认情况下,DozerMapper映射,设置为HashSet会破坏元素的顺序

[英]DozerMapper maps by default, Set to HashSet which destroys the ordering of elements

I have 2 DTOS namely OutputResponse and OutputDTO. 我有2个DTOS,即OutputResponse和OutputDTO。 The format of the class is as follow: 该类的格式如下:

class OutputDTO{

    private LinkedHashSet<String> items;
}

class OutputResponse{

    private Set<String> items;
}

When I try to use DozerMapper to map these 2 objects, it maps the destination to HashSet instead of LinkedHashSet. 当我尝试使用DozerMapper映射这2个对象时,它将目标映射到HashSet而不是LinkedHashSet。

class X{

    DozerBeanMapper mapper;

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

}

In the above scenario, the map() method maps the LinkedHashSet to HashSet which destroys the order of output response. 在上述情况下,map()方法将LinkedHashSet映射到HashSet,这会破坏输出响应的顺序。 Any suggestions regarding this? 有什么建议吗?

If you want the Set of items in OutputResponse to have some kind of order, you will need to use a different Collection implementation. 如果希望OutputResponse中的项目集具有某种顺序,则将需要使用其他Collection实现。 A Set is unordered by definition. 根据定义,集合是无序的。 List is ordered, as is LinkedHashSet. 列表是有序的,LinkedHashSet也是如此。 By defining the items as a Set, you have defined that there is no specific order, and Dozer has honored that specification by using HashSet, which is also unordered. 通过将项目定义为Set,您可以定义没有特定的顺序,并且Dozer通过使用HashSet兑现了该规范,该哈希也是无序的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM