IntPredicate
IntPredicate (int 프레디케이트)
하나의 int 값 인자에 대한 프레디케이트(boolean 값을 반환하는 함수)를 나타내는 함수형 인터페이스예요. Predicate의 int 소비 원시 타입 특수화예요.
본문
IntPredicate의 추상 메서드는 test(int)로 int 인자를 받아 boolean을 반환해요. 기본 메서드로 and, or, negate가 있어 결합·부정할 수 있어요.
@FunctionalInterface
public interface IntPredicate
IntPredicate even = n -> n % 2 == 0;
IntPredicate positive = n -> n > 0;
boolean r1 = even.test(4); // true
boolean r2 = even.and(positive).test(-2); // false
boolean r3 = even.negate().test(4); // false
IntStream.filter에 자주 사용돼요.
int sum = IntStream.range(1, 10)
.filter(n -> n % 2 == 0)
.sum(); // 2+4+6+8 = 20
함수형 메서드는 test(int)예요. 코드와 시그니처는 원문 그대로 보존돼요.