簡體   English   中英

是否可以使用MapStruct將Java中的HashMap轉換為List?

[英]Is it possible to convert HashMap in java to List using MapStruct?

我們正在嘗試找到一種使用mapstruct將HashMap轉換為List的方法,但是互聯網上沒有這種幫助。 有誰知道使用mapstruct的方法嗎?

我們嘗試定義抽象類並使用抽象映射,但沒有任何效果

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.WARN,
implementationPackage = "com.mapstruct.mapper.impl")
public abstract class OrderLineMapper {
  public com.internal.epfo.v1.OrderLine toOrderLineList(Map.Entry<Integer, OrderLine> orderLineEntry) {
    com.internal.epfo.v1.OrderLine orderLine = new com.internal.epfo.v1.OrderLine();
    orderLine.setCategoryTypeCode(orderLineEntry.getValue().getCategoryTypeCode());
    orderLine.getProducts().addAll(getProductInfoList(orderLineEntry.getValue().getProducts()));
    return orderLine;
  }

  List<com.internal.epfo.v1.ProductInfo> getProductInfoList(EnrichProductInfoMap<String, ProductInfo> products) {
    List<com.internal.epfo.v1.ProductInfo> productInfo = products.values().stream().collect(Collectors.toCollection( ArrayList<com.internal.epfo.v1.ProductInfo>::new ));
    return productInfo;
  }

  @MapMapping
  public abstract List<com.internal.epfo.v1.OrderLine> toOrderLineList(
      Map<Integer, OrderLine> orderLine);
}

無法生成從不可迭代類型到可迭代類型的映射方法。

沒有將Map轉換為List的開箱即用支持。 但是,您可以添加自定義方法。

public abstract class OrderLineMapper {
  public OrderLineV1 toOrderLine(Map.Entry<Integer, OrderLine> orderLineEntry) {
    OrderLineV1 orderLine = new OrderLineV1();
    orderLine.setCategoryTypeCode(orderLineEntry.getValue().getCategoryTypeCode());
    orderLine.getProducts().addAll(getProductInfoList(orderLineEntry.getValue().getProducts()));
    return orderLine;
  }

  List<ProductInfoV1> getProductInfoList(EnrichProductInfoMap<String, ProductInfo> products) {
    List<ProductInfoV1> productInfo = products.values().stream().collect(Collectors.toCollection( ArrayList<ProductInfoV1>::new ));
    return productInfo;
  }

  public List<OrderLineV1> toOrderLineList(Map<Integer, OrderLine> orderLine) {
    return orderLine == null ? null : toOrderLineList(orderLine.entrySet());
  }

  public abstract List<OrderLineV1> toOrderLineList(Collection<Map.Entry<Integer, OrderLine> orderLineCollection);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM