SeouliteLab

[Java/자바] BiFunction 연결: andThen() 메서드를 사용하여 BiFunction 순차 실행하기 본문

프로그래밍

[Java/자바] BiFunction 연결: andThen() 메서드를 사용하여 BiFunction 순차 실행하기

Seoulite Lab 2024. 3. 13. 14:26

Java에서는 BiFunction을 연결하여 순차적으로 실행할 수 있습니다. 이를 통해 여러 작업을 순차적으로 처리하고 결과를 얻을 수 있습니다. andThen() 메서드를 사용하여 BiFunction을 연결하는 방법을 알아보겠습니다.

예제 1: 두 개의 BiFunction을 연결하여 순차 실행하기

두 개의 BiFunction을 연결하여 순차적으로 실행하는 예제입니다. 첫 번째 BiFunction의 결과가 두 번째 BiFunction의 입력으로 전달됩니다.

import java.util.function.BiFunction;

public class BiFunctionChaining {
    public static void main(String[] args) {
        BiFunction<Integer, Integer, Integer> multiply = (a, b) -> a * b;
        BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;

        BiFunction<Integer, Integer, Integer> multiplyAndAdd = multiply.andThen(add);
        int result = multiplyAndAdd.apply(3, 5);
        System.out.println("결과: " + result); // 출력 결과: 결과: 18 (3 * 5 + 5)
    }
}

예제 2: 두 개의 BiFunction을 연결하여 순차 실행하기 (다른 데이터 형식)

두 개의 BiFunction을 연결하여 순차적으로 실행하는 예제입니다. 첫 번째 BiFunction의 결과가 두 번째 BiFunction의 입력으로 전달됩니다. 이 예제에서는 문자열과 정수를 처리하는 BiFunction을 사용합니다.

import java.util.function.BiFunction;

public class BiFunctionChaining {
    public static void main(String[] args) {
        BiFunction<String, Integer, String> append = (s, n) -> s + n;
        BiFunction<String, String, String> concatenate = (s1, s2) -> s1.concat(s2);

        BiFunction<String, Integer, String> appendAndConcatenate = append.andThen(concatenate);
        String result = appendAndConcatenate.apply("Number: ", 10);
        System.out.println("결과: " + result); // 출력 결과: 결과: Number: 10
    }
}

예제 3: andThen()을 여러 번 연속해서 사용하기

여러 개의 BiFunction을 순차적으로 연결하여 실행하는 예제입니다. 여러 개의 andThen() 메서드 호출로 BiFunction을 연결합니다.

import java.util.function.BiFunction;

public class BiFunctionChaining {
    public static void main(String[] args) {
        BiFunction<Integer, Integer, Integer> multiplyBy2 = (a, b) -> a * 2;
        BiFunction<Integer, Integer, Integer> addBy3 = (a, b) -> a + 3;
        BiFunction<Integer, Integer, Integer> divideBy2 = (a, b) -> a / 2;

        BiFunction<Integer, Integer, Integer> combinedFunction = multiplyBy2.andThen(addBy3).andThen(divideBy2);
        int result = combinedFunction.apply(10, 5);
        System.out.println("결과: " + result); // 출력 결과: 결과: 13 ((10 * 2 + 3) / 2)
    }
}

예제 4: BiFunction을 여러 번 연속해서 사용하기

여러 개의 BiFunction을 연속해서 사용하여 결과를 얻는 예제입니다. andThen() 메서드 대신 변수에 결과를 저장하여 연속적으로 적용합니다.

import java.util.function.BiFunction;

public class BiFunctionChaining {
    public static void main(String[] args) {
        BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
        BiFunction<Integer, Integer, Integer> subtract = (a, b) -> a - b;
        BiFunction<Integer, Integer, Integer> multiply = (a, b) -> a * b;

        int result1 = add.apply(10, 5);
        int result2 = subtract.apply(result1, 3);
        int finalResult = multiply.apply(result2, 2);

        System.out.println("결과: " + finalResult); // 출력 결과: 결과: 34 ((10 + 5) - 3) * 2
    }
}