Stream 매칭

최지혜·2022년 12월 4일
0

java

목록 보기
31/33

Stream API 매칭

allMatch()

모든 요소들이 매개값(Predicate)으로 주어진 조건을 만족하는지 조사

anyMatch()

최소한 한 개의 요소가 주어진 조건에 만족하는지 조사

noneMatch()

모든 요소들이 주어진 조건을 만족하지 않는지 조사

ex

public static void main(String[] args){
        int[] intArr = {2, 4, 6};

        boolean result = Arrays.stream(intArr)
                .allMatch(a -> a%2 == 0);
        System.out.println("2의 배수? " + result);

        result = Arrays.stream(intArr)
                .anyMatch(a -> a%3 == 0);
        System.out.println("3의 배수가 하나라도 있나? " + result);

        result = Arrays.stream(intArr)
                .noneMatch(a -> a%3 == 0);
        System.out.println("3의 배수가 없나? " + result);
}
  • 출력
    2의 배수? true
    3의 배수가 하나라도 있나? true
    3의 배수가 없나? false
profile
매일 성장하는 개발자

0개의 댓글