要在java 8中映射的对象列表 [英] List of objects to map in java 8
本文介绍了要在java 8中映射的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的课程
ObjectA
id
type
其中id是唯一的,但类型可以重复,所以如果我有像这样的ObjectA列表
where id is unique but the type can be duplicate so if I have a list of ObjectA like this
(id, type) = (1, "A") , (2, "B"), (3, "C"), (4, "A")
然后我想 Map< type,List< id> >
其中
map是
then I want Map<type, List<id>>
where
map is
< "A",[1,4],
"B",[2],
"C",[3] >
它正在使用Java 7但无法在Java 8中找到方法。 Java 8我可以创建键值对,但如果类型相同则无法创建列表。
It is working with Java 7 but can't find a way to do it in Java 8. In Java 8 I can create key, value pair but not able to create a list if a type is same.
推荐答案
yourTypes.stream()
.collect(Collectors.groupingBy(
ObjectA::getType,
Collectors.mapping(
ObjectA::getId, Collectors.toList()
)))
这篇关于要在java 8中映射的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文