문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 마지막 판 양쪽 다음 판 | ||
java:8:stream [2019/10/24 10:47] kwon37xi [unmodifiable, immutable collection] |
java:8:stream [2021/03/09 14:40] kwon37xi [Null safe stream] |
||
---|---|---|---|
줄 43: | 줄 43: | ||
</ | </ | ||
+ | |||
+ | ===== Collectors.toMap ===== | ||
+ | * '' | ||
+ | * 보통은 Key 하나에 값이 하나 일 때 사용. | ||
+ | * Key 하나에 값이 여러개가 나오는 충돌에 대해 '' | ||
+ | * 원래 '' | ||
+ | * 이유는 해당 구현 내부적으로 호출하는 '' | ||
+ | |||
+ | ===== Collectors.groupingBy ===== | ||
+ | * '' | ||
===== distinct by key ===== | ===== distinct by key ===== | ||
줄 84: | 줄 94: | ||
.collect(ImmutableList.toImmutableList()); | .collect(ImmutableList.toImmutableList()); | ||
</ | </ | ||
+ | |||
+ | <code java> | ||
+ | // Generic Immutable Collection collector 만들기. 컬렉션 구현체를 원하는대로 선택 | ||
+ | public static <T, A extends List< | ||
+ | Supplier< | ||
+ | | ||
+ | return Collector.of( | ||
+ | supplier, | ||
+ | List::add, (left, right) -> { | ||
+ | left.addAll(right); | ||
+ | return left; | ||
+ | }, Collections:: | ||
+ | } | ||
+ | |||
+ | // 사용예 - LinkedList 구현을 unmodifiable로 감싸기 | ||
+ | List< | ||
+ | List< | ||
+ | .collect(MyImmutableListCollector.toImmutableList(LinkedList:: | ||
+ | </ | ||
+ | * '' | ||
===== 참고 ===== | ===== 참고 ===== | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||