英文:
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<QuestionType, Profile>
to List<ProfileResponse>
using MapStruct (1.3.1)
@Mapper(componentModel = "spring")
public interface ProfileResponseMapper {
List<ProfileResponse> toProfileResponse(Map<QuestionType, Profile> 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 = "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);
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 ->
{
ProfileRespone respone = new ProfileResponse(key.type, val.name, val.id);
list.add(response);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论