BiPredicate
BiPredicate (두 인자를 받는 프레디케이트)
두 인자에 대한 프레디케이트(boolean 값을 반환하는 함수)를 나타내는 함수형 인터페이스예요. Predicate의 2-arity 특수화예요.
본문
BiPredicate<T,U>의 추상 메서드는 test(T, U)로 두 인자를 받아 boolean을 반환해요. 기본 메서드로 and, or, negate가 있고, 결합·부정 연산이 가능해요.
@FunctionalInterface
public interface BiPredicate<T,U>
BiPredicate<String, Integer> lengthOk =
(s, n) -> s.length() > n;
BiPredicate<String, Integer> containsA =
(s, n) -> s.contains("a");
boolean r1 = lengthOk.test("hello", 3); // true (5 > 3)
boolean r2 = lengthOk.and(containsA).test("abc", 2); // true
boolean r3 = lengthOk.negate().test("hi", 3); // true
Stream.filter에 두 인자를 쓰는 경우라기보다, Map을 순회하며 조건을 검사할 때 주로 사용해요.
Map<String, Integer> map = Map.of("a", 1, "b", 2);
BiPredicate<String, Integer> big = (k, v) -> v >= 2;
map.entrySet().removeIf(e -> big.test(e.getKey(), e.getValue()));
함수형 메서드는 test(Object, Object)예요. 코드와 시그니처는 원문 그대로 보존돼요.