I need to generate a unique friend list from a list that can have duplicates by merging all duplicate entries into one object
Example - Friends are fetched from different social feeds and put into 1 big list
1. Friend - [name: "Johnny Depp", dob: "1970-11-10", source: "FB", fbAttribute: ".."]
2. Friend - [name: "Christian Bale", dob: "1970-01-01", source: "LI", liAttribute: ".."]
3. Friend - [name: "Johnny Depp", dob: "1970-11-10", source: "Twitter", twitterAttribute: ".."]
4. Friend - [name: "Johnny Depp", dob: "1970-11-10", source: "LinkedIn", liAttribute: ".."]
5. Friend - [name: "Christian Bale", dob: "1970-01-01", source: "LI", liAttribute: ".."]
Expected output
1. Friend - [name: "Christian Bale", dob: "1970-01-01", liAttribute: "..", fbAttribute: "..", twitterAttribute: ".."]
2. Friend - [name: "Johnny Depp", dob: "1970-11-10", liAttribute: "..", fbAttribute: "..", twitterAttribute: ".."]
Question - How can i merge without using any intermediate container? I can easily use an intermediate map and apply reduce on each value of the entry.
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());
I like to do this without using the intermediate Map uniqueFriendMap. Any suggestions?