可能将HashMap转换为List使用MapStruct。

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

Is it possible to convert from HashMap to List using MapStruct

问题

我知道有一个类似的问题(参见问题),但我在那里没有找到有用的答案。假设我有以下的类:

class QuestionType {
    private String type;
}

class Profile {
    private String name;
    private int id;
}

class ProfileResponse {
    private String name;
    private int id;
    private String type;
}

我如何使用 MapStruct(1.3.1)将 HashMap<QuestionType, Profile> 转换为 List<ProfileResponse>

@Mapper(componentModel = "spring")
public interface ProfileResponseMapper {
    List<ProfileResponse> toProfileResponse(Map<QuestionType, Profile> profiles);
}
英文:

I know there is a similar question (see question), but I've found no useful answer there. Let's say I have the following classes:

class QuestionType {
    private String type;
}

class Profile {
    private String name;
    private int id;
}

class ProfileResponse {
    private String name;
    private int id;
    private String type;
}

How could I convert a HashMap&lt;QuestionType, Profile&gt; to List&lt;ProfileResponse&gt; using MapStruct (1.3.1)

@Mapper(componentModel = &quot;spring&quot;)
public interface ProfileResponseMapper {
    List&lt;ProfileResponse&gt; toProfileResponse(Map&lt;QuestionType, Profile&gt; profiles);
}

答案1

得分: 4

MapStruct没有针对将Map转换为List的隐式转换机制。但是,您可以使用一个技巧将Map视为Entry的集合,并将每个条目映射到列表项:

@Mapper(componentModel = "spring")
public interface ProfileResponseMapper {

    @Mapping(target = "type", source = "key.type")
    @Mapping(target = "name", source = "value.name")
    @Mapping(target = "id", source = "value.id")
    ProfileResponse map(Map.Entry<QuestionType, Profile> profile);
        
    default List<ProfileResponse> toProfileResponse(Map<QuestionType, Profile> map) {
        return map.entrySet()
                  .stream()
                  .map(this::map)
                  .collect(Collectors.toList());
    }
}
ProfileResponseMapper mapper = ...    // ProfileResponseMapper.INSTANCE or @Autowired 
Map<QuestionType, Profile> map = ...  // input Map

List<ProfileResponse> profileResponseList = mapper.toProfileResponse(map);

该代码利用了Java 8的default方法和Stream API的优势,否则可以使用abstract class和for-each循环。

英文:

MapStruct doesn't have an implicit conversion mechanism for mapping Map into List. However, you can do a trick to understand Map as a collection of Entry and map each entry into the list item:

@Mapper(componentModel = &quot;spring&quot;)
public interface ProfileResponseMapper {

    @Mapping(target = &quot;type&quot;, source = &quot;key.type&quot;)
    @Mapping(target = &quot;name&quot;, source = &quot;value.name&quot;)
    @Mapping(target = &quot;id&quot;, source = &quot;value.id&quot;)
    ProfileResponse map(Map.Entry&lt;QuestionType, Profile&gt; profile);
        
    default List&lt;ProfileResponse&gt; toProfileResponse(Map&lt;QuestionType, Profile&gt; map) {
        return map.entrySet()
                  .stream()
                  .map(this::map)
                  .collect(Collectors.toList());
    }
}
ProfileResponseMapper mapper = ...    // ProfileResponseMapper.INSTANCE or @Autowired 
Map&lt;QuestionType, Profile&gt; map = ...  // input Map

List&lt;ProfileResponse&gt; profileResponseList = mapper.toProfileResponse(map);

The code uses the advantage of Java 8 default methods and Stream API, otherwise use an abstract class and a for-each loop.

答案2

得分: 0

我更倾向于遵循KISS原则:
通过双消费者迭代地遍历映射,创建你的ProfileResponse对象并将它们放入一个列表中。

map.forEach((key, val) -> {
    ProfileResponse response = new ProfileResponse(key.type, val.name, val.id);
    list.add(response);
});
英文:

I would prefer to follow the KISS principle:
Iterate via a bi-consumer through the map, create your ProfileResponse Objects and put them to a list.

map.forEach (key, val -&gt; 
    {
        ProfileRespone respone = new ProfileResponse(key.type, val.name, val.id);
        list.add(response);
    }

huangapple
  • 本文由 发表于 2020年10月23日 18:33:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64498381.html
匿名

发表评论

匿名网友

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

确定