사용자 도구

사이트 도구


java:8:stream

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판 양쪽 다음 판
java:8:stream [2019/10/24 10:47]
kwon37xi [unmodifiable, immutable collection]
java:8:stream [2019/10/24 10:58]
kwon37xi [unmodifiable, immutable collection]
줄 83: 줄 83:
       .boxed()       .boxed()
       .collect(ImmutableList.toImmutableList());       .collect(ImmutableList.toImmutableList());
 +</code>
 +
 +<code java>
 +// Generic Immutable Collection collector 만들기. 컬렉션 구현체를 원하는대로 선택
 +public static <T, A extends List<T>> Collector<T, A, List<T>> toImmutableList(
 +  Supplier<A> supplier) {
 +  
 +    return Collector.of(
 +      supplier,
 +      List::add, (left, right) -> {
 +        left.addAll(right);
 +        return left;
 +      }, Collections::unmodifiableList);
 +}
 +
 +// 사용예 - LinkedList 구현을 unmodifiable로 감싸기
 +List<String> givenList = Arrays.asList("a", "b", "c", "d");
 +List<String> result = givenList.stream()
 +  .collect(MyImmutableListCollector.toImmutableList(LinkedList::new));
 </code> </code>
 ===== 참고 ===== ===== 참고 =====
   * [[https://www.baeldung.com/java-predicate-chain|Java 8 Predicate Chain | Baeldung]]   * [[https://www.baeldung.com/java-predicate-chain|Java 8 Predicate Chain | Baeldung]]
  
java/8/stream.txt · 마지막으로 수정됨: 2022/04/09 00:10 저자 kwon37xi