BiFunction

BiFunction (두 인자를 받는 함수형 인터페이스)

두 개의 인자를 받아 결과를 생성하는 함수를 나타내는 함수형 인터페이스예요. Function의 2-arity 특수화에요.

출처: Java API Reference

본문

BiFunction<T,U,R>의 추상 메서드는 apply(T, U)로 두 인자를 받아 결과 R을 반환해요.

@FunctionalInterface
public interface BiFunction<T,U,R>

기본 메서드 andThen(Function)으로 결과를 후처리하는 함수를 연결할 수 있어요.

BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
Function<Integer, String> toStr = n -> "결과: " + n;
String result = add.andThen(toStr).apply(10, 20); // "결과: 30"

대표적인 사용처로 Map.replaceAll, Map.merge 등이 있어요.

Map<String, Integer> scores = new HashMap<>();
scores.put("kim", 80);
scores.replaceAll((k, v) -> v + 5); // 모든 값에 5를 더함

함수형 메서드는 apply(Object, Object)예요. 코드와 시그니처는 원문 그대로 보존돼요.

더 알아보기