<aside> <img src="https://prod-files-secure.s3.us-west-2.amazonaws.com/83c75a39-3aba-4ba4-a792-7aefe4b07895/94dbaed9-349c-449d-bc43-25de3ae5a780/SpartaIconScale9.png" alt="https://prod-files-secure.s3.us-west-2.amazonaws.com/83c75a39-3aba-4ba4-a792-7aefe4b07895/94dbaed9-349c-449d-bc43-25de3ae5a780/SpartaIconScale9.png" width="40px" /> 수업 목표
<aside> <img src="https://prod-files-secure.s3.us-west-2.amazonaws.com/83c75a39-3aba-4ba4-a792-7aefe4b07895/393cd135-1603-4797-8fa6-42abcfedd782/SpartaIconS24.png" alt="https://prod-files-secure.s3.us-west-2.amazonaws.com/83c75a39-3aba-4ba4-a792-7aefe4b07895/393cd135-1603-4797-8fa6-42abcfedd782/SpartaIconS24.png" width="40px" />
목차
</aside>
<aside> <img src="https://prod-files-secure.s3.us-west-2.amazonaws.com/83c75a39-3aba-4ba4-a792-7aefe4b07895/11965e36-5cde-4d10-b470-06dfbe247327/scc캐릭터_아하_280x280.png" alt="https://prod-files-secure.s3.us-west-2.amazonaws.com/83c75a39-3aba-4ba4-a792-7aefe4b07895/11965e36-5cde-4d10-b470-06dfbe247327/scc캐릭터_아하_280x280.png" width="40px" /> 필수 프로그램 설치
프로그램 설치가이드를 따라 천천히 설치를 진행해주세요.
✅ OpenJDK 8버전 이상
✅ IntelliJ (Community)
✅ Google Chrome
</aside>
<aside> 📚
스트림이 무엇인지 알아봅시다.
스트림은 데이터를 효율적으로 처리할 수 있는 흐름입니다.데이터 준비 → 중간 연산 → 최종 연산 순으로 처리됩니다.컬렉션(List, Set 등)과 함께 자주 활용됩니다.map() 과 filter() 예시를 살펴보겠습니다.</aside>
<aside> 💡
각 요소를 10배로 변환 후 출력하는 예시로 알아봅시다.
arrayList 의 각 요소를 10배로 변환합니다.for 문과 스트림 을 비교해 봅시다.List<Integer> arrayList = new ArrayList<>(List.of(1, 2, 3, 4, 5));
public class Main {
public static void main(String[] args) {
// ArrayList 선언
List<Integer> arrayList = new ArrayList<>(List.of(1, 2, 3, 4, 5));
// ✅ for 명령형 스타일: 각 요소 * 10 처리
List<Integer> ret1 = new ArrayList<>();
for (Integer num : arrayList) {
int multipliedNum = num * 10; // 각 요소 * 10
ret1.add(multipliedNum);
}
System.out.println("ret1 = " + ret1);
}
}
public class Main {
public static void main(String[] args) {
// ArrayList 선언
List<Integer> arrayList = new ArrayList<>(List.of(1, 2, 3, 4, 5));
// ✅ 스트림 선언적 스타일: 각 요소 * 10 처리
List<Integer> ret2 = arrayList.stream().map(num -> num * 10).collect(Collectors.toList());
System.out.println("ret2 = " + ret2);
}
}
<aside> ❗
ArrayList 를 List 로 받는 이유
다형성을 활용해 List 인터페이스로 ArrayList 구현체를 받으면 나중에 다른 구현체(LinkedList , Vector) 로 변경할 때 코드 수정을 최소화할 수 있기 때문입니다.List 타입으로 받는 것을 권장합니다.List<Integer> arrayList = new ArrayList<>();
</aside>
</aside>
<aside> 📚
스트림 처리 단계를 살펴봅시다.
| 단계 | 설명 | 주요 API |
|---|---|---|
| 1. 데이터 준비 | 컬렉션을 스트림으로 변환 | stream(), parallelStream() |
| **2. 중간 연산 등록 | ||
| (즉시 실행되지 않음)** | 데이터 변환 및 필터링 | map(), filter(), sorted() |
| 3. 최종 연산 | 최종 처리 및 데이터 변환 | collect(), forEach(), count() |
| </aside> |