我需要通过将所有重复的条目合并到一个对象中从列表中生成一个唯一的朋友列表可以有重复项
示例 - 从不同的社交订阅源获取朋友并将其放入1个大列表中
1.朋友 - [姓名:"Johnny Depp", dob:"1970-11-10",来源:"FB",fbAttribute:".."]
2.朋友 - [姓名:"Christian Bale",dob:"1970-01-01",来源:"LI" ,liAttribute:".."]
3.朋友 - [姓名:"Johnny Depp",dob:"1970-11-10",来源:"Twitter",twitterAttribute:".."]
4.朋友 - [姓名: "Johnny Depp",dob:"1970-11-10",来源:"LinkedIn",liAttribute:".."]
5.朋友 - [姓名:"Christian Bale",dob:"1970-01-01",来源:"LI",liAttribute:".."]
预期输出
1.朋友 - [姓名:"Christian Bale",dob:"1970-01-01",liAttribute:"..",fbAttribute:"..",twitterAttribute:".."]
2.朋友 - [名称:"Johnny Depp",dob:"1970-11-10",liAttribute:"..",fbAttribute:"..",twitterAttribute:".."]
问题 - 如何在不使用任何中间容器的情况下合并?我可以轻松地使用中间映射并对条目的每个值应用reduce.
List<Friend> friends;
Map<String, List<Friend>> uniqueFriendMap
= friends.stream().groupingBy(Friend::uniqueFunction);
List<Friend> mergedFriends = uniqueFriendMap.entrySet()
.stream()
.map(entry -> {
return entry.getValue()
.stream()
.reduce((a,b) -> friendMergeFunction(a,b));
})
.filter(mergedPlace -> mergedPlace.isPresent())
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我喜欢这样做而不使用中间Map uniqueFriendMap.有什么建议?