简体   繁体   English

推土机将HashMap <Key,Value>映射到List <Value>

[英]Dozer Mapping HashMap<Key,Value> to List<Value>

I have a source object which has: 我有一个源对象,它具有:

public class Source {
    public Map<String,DTO>getDTOs();
}

and a destination object: 和目标对象:

public class Destination {
    public List<DTO> getDTOs();
    public void setDTOs(List<DTO> dtos);
}

I'm trying to use Dozer to do this mapping for me, but I'm sort of stumped. 我正在尝试使用Dozer为我做这个映射,但我有点难过。 I've triaged the unit tests and only documents, but my challenge is that I'm not exactly sure what I'm looking for. 我对单元测试和文档进行了分类,但我的挑战是我不确定我在寻找什么。

In general, Dozer enjoys mapping like to like, especially as far as data structures go. 一般来说,Dozer喜欢映射,就像数据结构一样。 This makes your problem a tricky one. 这使你的问题变得棘手。

No doubt you have read up on mapping java.util.Map to java.util.Map. 毫无疑问,您已经阅读了将java.util.Map映射到java.util.Map的内容。 That does work great if you have Maps on both ends. 如果你有两端的地图,这确实很有用。 Unfortunately, all the kings 'map-set-method's and all the kings men are not likely going to help you with doing a java.util.Map to java.util.List conversion. 不幸的是,所有国王的map-set-method和所有的国王都不太可能帮助你做java.util.Map到java.util.List的转换。 Dozer wants to Map.get(key) from the source and Map.put(key,value) into the destination. Dozer希望从源代码Map.get(key)和Map.put(key,value)到目标。 Since List doesn't play the put game, you're jammed (I wasn't able to successfully fake it out with an 'add()' ... maybe there is a way?). 由于List不玩put游戏,你被卡住了(我无法用'add()'成功伪装它......也许有办法?)。

At the bottom of http://dozer.sourceforge.net/documentation/customconverter.html there is a section on "Data Structure Conversions", which seems to resemble your problem. http://dozer.sourceforge.net/documentation/customconverter.html的底部有一个关于“数据结构转换”的部分,它似乎与您的问题类似。 When the answer is "write a custom mapping", I'm probably not being a great help, but I got your code to work with the following setup. 当答案是“编写自定义映射”时,我可能不是一个很好的帮助,但我得到了您的代码以使用以下设置。

Have you had any luck with doing this all in dozer xml? 在dozer xml中你有这么多运气吗?

 <mapping>
    <class-a>Source</class-a>
    <class-b>Destination</class-b>
    <field custom-converter="HashMapToListConverter">
      <a>dtos</a> <!-- This is a Map<String,DTO> -->
      <b>dtos</b> <!-- This is a List<DTOPrime> -->
      <a-hint>java.util.LinkedHashMap</a-hint>
      <b-hint>java.util.ArrayList</b-hint>
    </field>
  </mapping>



import org.dozer.*;
import java.util.*;

public class HashMapToListConverter extends DozerConverter<Map, List> implements MapperAware {

  private Mapper mapper;

  public HashMapToListConverter() {
    super(Map.class, List.class);
  }

  public List convertTo(Map source, List destination) {
    List<DTOPrime> convertedList = new ArrayList<DTOPrime>();
    for (Object object : source.values()) {
      DTO item = (DTO)object;
      DTOPrime mappedItem = mapper.map(item, DTOPrime.class);
      convertedList.add(mappedItem);
    }
    return convertedList;
  }

  public Map convertFrom(List source, Map destination) {
    Map<String, DTO> originalToMapped = new LinkedHashMap<String, DTO>();
    for (Object object : source) {
      DTOPrime item = (DTOPrime)object;
      DTO mappedItem = mapper.map(item, DTO.class);
      originalToMapped.put(mappedItem.getData(), mappedItem); // FIXME: Since the mapping is lossy, you can decide what the keys of your map are for the reverse mapping...
    }
    return originalToMapped;
  }

  public void setMapper(Mapper mapper) {
    this.mapper = mapper;
  }

}

Have you tried? 你有没有尝试过?

destination.setDTOs(new ArrayList<DTO>(source.getDTOs().values()));

It s not clear what you are trying to do. 目前尚不清楚你要做什么。

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

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