나는 하나의 객체
예에 모든 중복 항목을 병합하여 중복을 가질 수 목록에서 고유 한 친구 목록을 생성해야 - 친구는 다른 소셜 피드 페치 1에 넣어 큰 목록
1. 친구 - [이름 : "Johnny Depp", dob : "1970-11-10", 출처 : "FB", fbAttribute : ".."]
2. 친구 - [이름 : "Christian 012. [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. 친구 - [이름 :"크리스찬 베일 ", 생년월일 :"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());
나는 중간지도 uniqueFriendMap를 사용하지 않고이 일을 좋아한다. 어떤 제안?
안녕하세요. Holger 님, 아마도 'Collectors.groupingBy'를'Collectors.toMap'과 같이 'Map uniqueFriendMap = friends.stream(). collect (Collectors.toMap (Friend :: uniqueFunction, Function.identity(), this :: friendMergeFunction))', 훨씬 더 간단하다고 생각하지 않습니까? –