SeouliteLab

[Java/자바] Function 연결: andThen()으로 다수의 Function 순차적 수행하기 본문

프로그래밍

[Java/자바] Function 연결: andThen()으로 다수의 Function 순차적 수행하기

Seoulite Lab 2024. 3. 13. 14:28

Java에서는 Function을 andThen() 메서드를 사용하여 순차적으로 연결하여 실행할 수 있습니다. 여러 개의 Function을 연결하여 복잡한 데이터 처리를 간결하게 표현할 수 있습니다.

예제 1: 두 개의 Function 순차적으로 실행하기

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

import java.util.function.Function;

public class FunctionChaining {
    public static void main(String[] args) {
        Function<Integer, Integer> addOne = n -> n + 1;
        Function<Integer, Integer> multiplyByTwo = n -> n * 2;

        Function<Integer, Integer> addOneAndMultiplyByTwo = addOne.andThen(multiplyByTwo);
        int result = addOneAndMultiplyByTwo.apply(5);
        System.out.println("결과: " + result); // 출력 결과: 결과: 12 ((5 + 1) * 2)
    }
}

예제 2: 세 개의 Function 순차적으로 실행하기

세 개의 Function을 순차적으로 연결하여 실행하는 예제입니다. 각 Function은 이전 Function의 결과를 입력으로 받습니다.

import java.util.function.Function;

public class FunctionChaining {
    public static void main(String[] args) {
        Function<Integer, Integer> addTwo = n -> n + 2;
        Function<Integer, Integer> multiplyByThree = n -> n * 3;
        Function<Integer, Integer> subtractFive = n -> n - 5;

        Function<Integer, Integer> addTwoThenMultiplyByThreeThenSubtractFive = addTwo.andThen(multiplyByThree).andThen(subtractFive);
        int result = addTwoThenMultiplyByThreeThenSubtractFive.apply(10);
        System.out.println("결과: " + result); // 출력 결과: 결과: 37 (((10 + 2) * 3) - 5)
    }
}

예제 3: 여러 개의 Function 순차적으로 실행하기 (andThen() 메서드 연속 사용)

여러 개의 Function을 연속적으로 andThen() 메서드로 연결하여 실행하는 예제입니다.

import java.util.function.Function;

public class FunctionChaining {
    public static void main(String[] args) {
        Function<Integer, Integer> addTwo = n -> n + 2;
        Function<Integer, Integer> multiplyByThree = n -> n * 3;
        Function<Integer, Integer> subtractFive = n -> n - 5;

        Function<Integer, Integer> combinedFunction = addTwo.andThen(multiplyByThree).andThen(subtractFive);
        int result = combinedFunction.apply(15);
        System.out.println("결과: " + result); // 출력 결과: 결과: 42 (((15 + 2) * 3) - 5)
    }
}

예제 4: 여러 개의 Function 순차적으로 실행하기 (변수에 결과 저장)

여러 개의 Function을 순차적으로 연결하여 결과를 변수에 저장하여 실행하는 예제입니다.

import java.util.function.Function;

public class FunctionChaining {
    public static void main(String[] args) {
        Function<Integer, Integer> addTwo = n -> n + 2;
        Function<Integer, Integer> multiplyByThree = n -> n * 3;
        Function<Integer, Integer> subtractFive = n -> n - 5;

        int intermediateResult = addTwo.apply(20);
        intermediateResult = multiplyByThree.apply(intermediateResult);
        int finalResult = subtractFive.apply(intermediateResult);

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

예제 5: 람다 표현식을 사용하여 Function 연결하기

람다 표현식을 사용하여 Function을 직접 정의하여 연결하는 예제입니다.

import java.util.function.Function;

public class FunctionChaining {
    public static void main(String[] args) {
        Function<Integer, Integer> addOneThenMultiplyByTwo = ((Function<Integer, Integer>) n -> n + 1)
                .andThen(n -> n * 2);

        int result = addOneThenMultiplyByTwo.apply(7);
        System.out.println("결과: " + result); // 출력 결과: 결과: 16 ((7 + 1) * 2)
    }
}