[문제]
정수 리스트에서 3의 배수만 필터링하여 새 리스트를 만들고 출력하세요.
[데이터]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15]
[요구사항]
- filter()로 3의 배수만 선택
- collect(Collectors.toList())로 새 리스트 생성
- 결과 출력
[소스]
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamToList {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15);
System.out.println("원본 숫자: " + numbers);
// filter()로 3의 배수 선택 후 collect()로 새 List 생성
List<Integer> multiplesOf3 = numbers.stream()
.filter(n -> n % 3 == 0) // 3의 배수
.collect(Collectors.toList()); // List로 변환
System.out.println("3의 배수: " + multiplesOf3);
}
}
다음검색