java随机动态分组,Java 8 Stream“收集并分组”映射到多个键的对象

I have the following objects:

public class Item {

String value;

List owners;

Person creator;

}

public class Person {

String name;

int id;

Person manager;

}

now i have the a list containing 3 Item objects:

i1 -> {value="1", owners=[p1, p2, p3], creator=p4}

i2 -> {value="2", owners=[p2, p3], creator=p5}

i3 -> {value="3", owners=[p5], creator=p1}

the Person objects are as follows:

p1 -> {manager=m1, ...}

p2 -> {manager=m2, ...}

p3 -> {manager=m3, ...}

p4 -> {manager=m2, ...}

p5 -> {manager=m1, ...}

I want to group the stream of Item objects based on managers of the owners and creator. The result Map> should look like:

{

m1: [i1, i2, i3],

m2: [i1, i2],

m3: [i1, i2]

}

I think using Stream and Collector APIs, I can make the map from Item to managers, Map> first, then reverse the mapping. But is there any way to make the mapping I want using Stream and Collectors only?

解决方案

I think, this is only possible with an intermediate “pair” value to remember the association between a person/manager and the originating item. In absence of a standard pair type in Java’s standard API, we have to resort to Map.Entry which comes closest to a Pair type:

Map> map = list.stream()

.flatMap(item->item.getOwners().stream()

.map(p->new AbstractMap.SimpleEntry<>(p.getManager(), item)))

.collect(Collectors.groupingBy(Map.Entry::getKey,

Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

after improving it using import static we get

Map> map = list.stream()

.flatMap(item->item.getOwners().stream().map(p->new SimpleEntry<>(p.getManager(), item)))

.collect(groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toList())));

It results in

m1: [i1,i3]

m3: [i1,i2]

m2: [i1,i2]

which differs because first, the standard map has no defined ordering, second, I think you made a mistake regarding you expectation as m1 is not associated with i2 in your example data.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值