최종처리 단계에서 요소들이 특정 조건에 만족여부 확인
- 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
'Programming Language > JAVA' 카테고리의 다른 글
자바 스트림 API - 최종처리(3)_collect (0) | 2023.09.08 |
---|---|
자바 스트림 API - 최종처리(2)_IntStream, LongStream, DoubleStream의 집계메서드 (0) | 2023.09.08 |
자바 스트림 API- 중간처리(3)_boxed/asDoubleStream() (0) | 2023.09.07 |
자바 스트림 API - 중간처리(2)_sorted/skip/limit (0) | 2023.09.07 |
자바 스트림 API - 중간처리(1)_distinct/filter/map/mapToInt/flatmap (1) | 2023.09.07 |