CHAPTER5 - QnA #55
-
CHAPTER5 - QnAQ1) 스트림 체이닝 메서드 중간 결과 (flatMap을 이해하기 전)List<String> userNames = Arrays.asList("CoRock", "ding_cook");
userNames.stream() // (1)
.map(name -> name.split("")) // (2)
.map(Arrays::stream) // (3)
.distinct() // (4)
.collect(toList()); // (5) (1), (2), (3), (4), (5) 까지의 결과에서 각각 타입은? Q2) 자바에서 무한 스트림을 만들 수 있는 메서드로 iterate와 generate 가 있다. 이 두 메서드의 차이 |
Beta Was this translation helpful? Give feedback.
Answered by
BvrPark
Jul 3, 2023
Replies: 1 comment 2 replies
-
|
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
JoisFe
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
stream()
은 List를 Stream으로 반환하기 때문에 타입은Stream<String>
이 될 것 같습니다.map()
은 각 요소들을 Stream으로 매핑하는데 split()은 문자열을 배열로 분할하기 때문에 반환 타입은Stream<String[]>
이 될 것 같습니다.map()
은 Arrays::stream을 호출하기 때문에 각 문자열 배열에 대해 Arrays.stream()를 부르게 됩니다. 그래서 반환 타입은Stream<Stream<String>>
입니다.distinct
는 중복요소를 제거할 뿐, 들어온 타입 그대로 반환하기 때문에 3번과 같은Stream<Stream<String>>
입니다.collect
는 들어온 스트림을 바탕으로 새로운 컬렉션을 만들어서 반환하는데 toList가 List로 구성요소들을 반환(?)시켜주는 메서드 이므로 반환값은List<Stream<String>>
일 것 같습니다.