I need to create a unique list of friends from a list that can have duplicates, combining all the duplicate entries into one object
Example . Friends are obtained from different social channels and placed on one big list
1. Friend - [name: "Johnny Depp", ext: "1970-11-10", source: "FB", fbAttribute: ".."]
2. Friend - [name: "Christian Bale", ext: "1970-01-01", source: "LI", liAttribute: ".."]
3. Friend - [name: "Johnny Depp", ext: "1970-11 -10 ", source:" Twitter ", twitterAttribute:" .. "]
4. Friend - [name:" Johnny Depp ", ext:" 1970-11-10 ", source:" LinkedIn ", liAttribute:" .. "]
5. Friend - [name:"Christian Bale", dob: "1970-01-01", source: "LI", liAttribute: ".."]
Expected Result
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 combine without using an intermediate container? I can easily use an intermediate map and apply a shorthand for each record value.
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 uniqueFriendMap middleware. Any suggestions?