Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 가입
- 프로그래밍
- 교보생명
- 리스트
- 급성심근경색증
- 웹개발
- 인출수수료
- 납입
- 문자열
- 심장질환
- jQuery
- 보험료
- 파이썬
- 특약
- 수수료
- 추가납입
- Vue.js
- 보험
- Java
- 뇌출혈
- 사망
- python
- javascript
- 프론트엔드
- 중도인출
- 교보
- 코딩
- 자바스크립트
- 변환
- PythonProgramming
Archives
- Today
- Total
SeouliteLab
[Java/자바] Function 연결: andThen()으로 다수의 Function 순차적 수행하기 본문
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)
}
}
'프로그래밍' 카테고리의 다른 글
[Java/자바] Spring RestTemplate을 활용한 웹 서비스 통신 (0) | 2024.03.14 |
---|---|
[Java/자바] Function 합성: compose() 메서드로 함수 합성하기 (0) | 2024.03.13 |
[Java/자바] Function 예제: 입력을 받아 결과를 반환하는 함수 (0) | 2024.03.13 |
[Java/자바] BiFunction 연결: andThen() 메서드를 사용하여 BiFunction 순차 실행하기 (0) | 2024.03.13 |
[Java/자바] BiFunction 예제: 두 개의 인수를 받아 결과를 반환하는 함수 (0) | 2024.03.13 |