Open sure-why-not opened 2 years ago
.flatMap()
은 Array나 Object로 감싸져 있는 모든 원소를 단일 원소 스트림으로 반환해준다.
String 이 아닌 2차원 배열을 예시로 들어보자.
int[][] sample = new int[][]{
{1, 2},
{3, 4},
{5, 6},
{7, 8},
{9, 10}
};
IntStream intStream = Arrays.stream(sample)
.flatMapToInt(array -> Arrays.stream(array));
intStream.forEach(System.out::println);
결과
1
2
3
4
5
6
7
8
9
10
문제
flatMap 은 어떤 상황에서 사용하는 것일까? 어떻게 동작하는 것일까?
선정 배경
map 을 주로 사용했기 때문에 flatMap 이 생소한데, 책의 예제만으로는 명확하게 이해되지 않아서 추가적인 정리와 실습을 해보면 좋을 것 같다.
관련 챕터