문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
java:8:stream [2017/11/21 15:29] kwon37xi [Null safe stream] |
java:8:stream [2022/04/09 00:10] kwon37xi [parallelStream] |
||
---|---|---|---|
줄 43: | 줄 43: | ||
</ | </ | ||
+ | |||
+ | ===== Collectors.toMap ===== | ||
+ | * '' | ||
+ | * 보통은 Key 하나에 값이 하나 일 때 사용. | ||
+ | * Key 하나에 값이 여러개가 나오는 충돌에 대해 '' | ||
+ | * 원래 '' | ||
+ | * 이유는 해당 구현 내부적으로 호출하는 '' | ||
+ | |||
+ | ===== Collectors.groupingBy ===== | ||
+ | * '' | ||
===== distinct by key ===== | ===== distinct by key ===== | ||
줄 48: | 줄 58: | ||
* [[https:// | * [[https:// | ||
public static <T> Predicate< | public static <T> Predicate< | ||
- | Map< | + | Map< |
return t -> seen.putIfAbsent(keyExtractor.apply(t), | return t -> seen.putIfAbsent(keyExtractor.apply(t), | ||
} | } | ||
줄 60: | 줄 70: | ||
.values(); | .values(); | ||
</ | </ | ||
+ | |||
+ | ===== parallelStream ===== | ||
+ | * [[https:// | ||
+ | * '' | ||
+ | * DB 조회/API 호출 처럼 순차보다는 병렬이 확실히 더 확실할 때 빼고는 '' | ||
+ | * 그리고 Thread 안정성도 보장해줘야한다. | ||
+ | * 따라서 성능테스트로 완벽하게 더 좋은 성능이 보장되지 않는다면, | ||
+ | |||
+ | ===== unmodifiable, | ||
+ | * [[https:// | ||
+ | |||
+ | <code java> | ||
+ | List< | ||
+ | | ||
+ | | ||
+ | Collections:: | ||
+ | </ | ||
+ | * [[https:// | ||
+ | |||
+ | <code java> | ||
+ | List< | ||
+ | .boxed() | ||
+ | .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:// | ||
+ |