[문제]
리스트에 100 이상인 숫자가 존재하는지 확인하세요.
[데이터]
[10, 20, 30, 40, 50] 와 [10, 20, 100, 40]
[요구사항]
- anyMatch() 메서드 사용
- 100 이상인 숫자 존재 여부 출력
[소스]
import java.util.Arrays;
import java.util.List;
public class StreamAnyMatch {
public static void main(String[] args) {
List<Integer> numbers1 = Arrays.asList(10, 20, 30, 40, 50);
List<Integer> numbers2 = Arrays.asList(10, 20, 100, 40);
System.out.println("숫자 리스트 1: " + numbers1);
// anyMatch()는 조건을 만족하는 요소가 하나라도 있으면 true
boolean hasOver100_1 = numbers1.stream()
.anyMatch(n -> n >= 100); // 100 이상인 요소가 있는가?
System.out.println("100 이상인 숫자 존재: " + hasOver100_1);
System.out.println("\n숫자 리스트 2: " + numbers2);
boolean hasOver100_2 = numbers2.stream()
.anyMatch(n -> n >= 100);
System.out.println("100 이상인 숫자 존재: " + hasOver100_2);
}
}