[ Java ] Stream Api의 map vs flatMap
![[ Java ] Stream Api의 map vs flatMap](https://cdn.hashnode.com/res/hashnode/image/upload/v1737274959744/7925979b-682e-47e3-8367-780b8a42de24.png)
목표 : Stream API의 map과 flatMap의 차이점과 각각의 활용 사례를 예시 코드와 함께 확인해보자.
1️⃣StreamApi의 Map ?
먼저 코드를 통해 확인하자.
//Map
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
List<Integer> list=Stream.of(br.readline()
.split(" ")).map(Integer::parseInt)
.toList();
System.out.println(list.toString());
}
}
Stream.of를 통해 Stream을 생성해줍니다.
map을 통해 입력받은 숫자들을 Integer.parseInt를 적용해서 int로 바꿔줍니다. 즉, map을 통해 원하는 요소를 뽑아낼 수 있습니다.
사례:단순 데이터 변환
2️⃣ Stream Api 의 flatMap?
public class Main {
public static void main(String[] args) throws IOException {
List<List<Integer>> nestedList = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList(4, 5, 6),
Arrays.asList(7, 8, 9)
);
List<Integer> listWithFlatMap = nestedList.stream()
.flatMap(List::stream)
.toList();
System.out.println(listWithFlatMap.toString());
}
}
nestedList.stream()을 통해 스트림을 생성해준다.
flatMap(List::stream)을 통해서 내부의 리스트를 개별 스트림으로 변환해준다. → 모든 요소들을 하나로 합친 단일 스트림으로 반환한다.
그 후 결과는 [1,2,3,4,5,6,7,8,9] 가 나온다.
사례: 중첩 컬렉션 처리, Optional체이닝, 다대다 관계 처리
⚡ 정리
차이점
Map : map()은 스트림의 각 요소를 변환하여 새로운 요소로 매핑한다.
flatMap : 각 요소를 스트림으로 변환한 후, 모든 스트림을 하나의 스트림으로 평면화한다.
(출처 : https://observerlife.tistory.com/98, https://s7won.tistory.com/5)
![[Spring] N+1문제 발생과 분석](https://cdn.hashnode.com/res/hashnode/image/upload/v1756727965704/8c24d83c-1a9e-4b4d-b733-c130243cbc6b.png)

![[Project] 날씨에 맞는 옷 추천 서비스 : 지그재그 크롤링 여정 기록 (1) ChromeDriver를 EC2에 설치하기](https://cdn.hashnode.com/res/hashnode/image/upload/v1753843082352/c2452b33-97a4-4148-8ba4-750e5eee6aff.png)
![[Project] 날씨에 맞는 옷 추천 프로젝트: Selenium은 정말 필요한 선택이었을까? - 크롤링 삽질 기록](https://cdn.hashnode.com/res/hashnode/image/upload/v1753343114273/b32cc35e-a2e6-4085-a132-26c72f8792d9.png)
![[Spring] @Cacheable, @CachePut, @CacheEvict](https://cdn.hashnode.com/res/hashnode/image/upload/v1749265456525/51b97bad-f86e-4f0f-9b33-77eaa733176f.png)