[Java] 스트림(Stream) 자료형 변환

서재·2023년 9월 25일

[Java] 스트림(Stream)

목록 보기
1/4

✅ 목표

List<String>List<Integer>로 변환


📕 without Stream

List<Integer> integerList = new ArrayList<>();

for (String stringValue : stringList) {
	int integerValue = Integer.parseInt(stringValue);
    integerList.add(integerValue);
}

📘 with Stream

List<Integer> integerList = stringList.stream()
        .map(Integer::parseInt)					//.map(e -> Integer.parseInt(e))
        .collect(Collectors.toList());

📌 .map(e -> {})

원본 스트림의 각 요소에 람다 함수를 적용한 값을 가지는 새로운 스트림을 생성

📌 .collect(Collector)

스트림의 종료 연산 중 하나
스트림의 요소들을 수집하여 다른 자료구조로 변환

📌 Collector

Collectors.toList() : 리스트로 수집
Collectors.toSet() : 세트로 수집
Collectors.toMap(keyMapper, valueMapper) : 맵으로 수집
Collectors.joining(String) : 문자열로 결합

profile
입니다.

0개의 댓글