Java/함수형 인터페이스

[Java]BiFunction 인터페이스 사용 방법

DevStory 2022. 8. 23.

BiFunction 인터페이스

BiFunction Interface는 Java에서 함수형 프로그래밍을 구현하기 위해 Java 버전 1.8부터 도입된 함수형 인터페이스로 두 개의 매개변수를 전달받아 특정 작업을 수행 후 새로운 값을 반환하는 경우 사용됩니다.

 

제네릭 타입인 두 개의 인수가 존재하며, 제네릭 타입을 반환합니다.

@FunctionalInterface
public interface BiFunction<T, U, R> {
  R apply(T t, U u);

  default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
    Objects.requireNonNull(after);
    return (T t, U u) -> after.apply(apply(t, u));
  }
}

BiFunction 인터페이스는 세 개의 제네릭 타입을 사용합니다.

T: 첫 번째 매개변수의 타입입니다.

U: 두 번째 매개변수의 타입입니다.

R: 반환 타입입니다.

 

인터페이스 내부에는 추상 메서드 apply()와 디폴트 메서드인 andThen() 메서드가 존재합니다.

 

람다 표현식을 사용하면 BiFunction 인터페이스의 추상 메서드를 구현하는 클래스를 정의할 필요가 없으며, BiFunction 타입의 객체에 할당된 람다 표현식은 apply() 메서드를 구현하기 위해 사용됩니다.


BiFunction 인터페이스의 apply 메서드

apply() 메서드는 제네릭 타입인 두 개의 매개변수를 전달받아 특정 작업을 수행 후 값을 반환합니다.

R apply(T t, U u);

다음 예제는 apply() 메서드 사용 방법입니다.

 

먼저, 세 개의 BiFunction 타입의 객체를 생성하며 특정 로직을 수행하는 람다 표현식을 할당합니다.

- biFunctionAdd 객체는 Integer 타입의 두 개의 매개변수를 전달받아 더한 결과를 문자열로 반환합니다.

- biFunctionMinus 객체는 Integer 타입의 두 개의 매개변수를 전달받아 뺄셈 결과를 문자열로 반환합니다.

- biFunctionMultiple 객체는 Integer 타입의 두 개의 매개변수를 전달받아 곱한 결과를 문자열로 반환합니다.

public static void main(String args[]) {
  BiFunction<Integer, Integer, String> biFunctionAdd =
          (num1, num2) ->  Integer.toString(num1 + num2);

  BiFunction<Integer, Integer, String> biFunctionMinus =
          (num1, num2) -> Integer.toString(num1 - num2);

  BiFunction<Integer, Integer, String> biFunctionMultiple =
          (num1, num2) -> Integer.toString(num1 * num2);

  System.out.println("100 + 50 = " + biFunctionAdd.apply(100, 50));
  System.out.println("100 - 50 = " + biFunctionMinus.apply(100, 50));
  System.out.println("100 * 50 = " + biFunctionMultiple.apply(100, 50));
}

[실행 결과]

100 + 50 = 150
100 - 50 = 50
100 * 50 = 5000

BiFunction 인터페이스의 andThen 메서드

apply() 메서드 실행 후 반환 결과에 대해 특정 작업이 필요한 경우 해당 로직을 andThen() 메서드에 전달합니다.

 

매개변수로 전달되는 함수는 BiFunction 타입이 아니라 Function 타입이어야 합니다.

 

다음 예제는 apply() 메서드 반환 결과를 andThen() 메서드를 사용하여 문자열을 병합합니다.

public static void main(String args[]) {
  BiFunction<Integer, Integer, String> biFunctionAdd =
          (num1, num2) ->  Integer.toString(num1 + num2);
  biFunctionAdd = biFunctionAdd.andThen(result -> "biFunctionAdd Result: " + result);

  BiFunction<Integer, Integer, String> biFunctionMinus =
          (num1, num2) -> Integer.toString(num1 - num2);
  biFunctionMinus = biFunctionMinus.andThen(result -> "biFunctionMinus Result: " + result);

  BiFunction<Integer, Integer, String> biFunctionMultiple =
          (num1, num2) -> Integer.toString(num1 * num2);
  biFunctionMultiple = biFunctionMultiple.andThen(result -> "biFunctionMultiple Result: " + result);

  System.out.println(biFunctionAdd.apply(100, 50));
  System.out.println(biFunctionMinus.apply(100, 50));
  System.out.println(biFunctionMultiple.apply(100, 50));
}

[실행 결과]

biFunctionAdd Result: 150
biFunctionMinus Result: 50
biFunctionMultiple Result: 5000
반응형

댓글