티스토리 뷰

최종처리 단계에서 요소들이 특정 조건에 만족여부 확인

  • allMatch(Predicate): 모든 요소들이 Predicate 조건에 일치하는지 체크
  • anyMatch(Predicate): 일부분의 요소들이 Predicate 조건에 일치하는지 체크
  • noneMatch(Predicate): 모든 요소들이 Predicate 조건에 일치하지 않는지 체크

 

=> 최종 결과는 boolean 값으로 반환




---


int[] arr = {1,2,3,4,5};




allMatch(Predicate)

//익명클래스
        IntPredicate predicate = new IntPredicate() {
            @Override
            public boolean test(int t) {
                return t < 10;
            }
        };

//모든 요소가 10보다 작냐?
        boolean result = Arrays.stream(arr)
                               .allMatch(predicate);
        System.out.println("모든 요소가 10보다 작냐?: " + result);

//람다표현식
        result = Arrays.stream(arr)
                       .allMatch(t->t < 10);
        System.out.println("모든 요소가 10보다 작냐?: " + result);

//모든 요소가 10보다 작냐?: true




anyMatch(Predicate)

//요소 중에서 3의 배수가 있냐?
        boolean result2 = Arrays.stream(arr)
                                .anyMatch(t->t%3==0);
        System.out.println("요소중에서 3의 배수가 있냐?: " + result2);

//요소중에서 3의 배수가 있냐?: true




noneMatch(Predicate)

//요소 중에서 3의 배수가 없냐?
        boolean result3 = Arrays.stream(arr)
                                .noneMatch(t->t%3==0);
        System.out.println("요소중에서 3의 배수가 없냐?: " + result3);

//요소중에서 3의 배수가 없냐?: false
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함