我有以下关系:A类具有B类对象列表,B类具有C类对象和D类对象.C类具有字符串E,D类具有整数F.Class C到D类是一对多的关系。
根据<String, List<A>>
的地图,如何将其重建为<F, E>
的地图?
提前致谢!
答案 0 :(得分:1)
String
中的Map<String, List<A>>
是否与输出相关?我假设您希望从原始地图中获取每对<F, E>
吗?所以这可能会有所帮助
Map<String, List<A>> input;
input.values().stream()
.flatMap(Collection::stream)
.map(a -> a.getListB()) // extract list B from A
.flatMap(Collection::stream) // Here you get all B instances
.collect(
toMap(
b -> b.getD().getF(), b -> b.getC().getE(),
(e1, e2) -> ??? // Here you should define your own merge function if there are two B instances has same F values
)
);