- 하나의 입력을 받아서 로직을 거쳐 특정 값을 반환하는 역할을 하는 함수형 인터페이스이다.
- apply() 추상 메소드를 가지고 있으며, andThen() 기본 메소드를 지원한다.
- 주의 사항으로는 Consumer의 andThen과 달리 이 andThen은 먼저 실행한 함수의 리턴값을 뒷 메소드의 입력으로 사용한다.
- 기본 구성은 Function<입력 타입, 반환 타입>와 같다.
public class FunctionExample {
public static void main(String[] args) {
Function<String, Integer> toInteger = s -> Integer.parseInt(s);
Integer result = toInteger.apply("123");
System.out.println(result);
Function<Integer, String> toString = s -> s.toString();
// andThen에서 toInteger의 반환 타입 Integer를 입력으로 받아야 한다.
String last = toInteger.andThen(toString).apply("8");
System.out.println(last);
}
}
'JAVA > JAVA8' 카테고리의 다른 글
UnaryOperator & BinaryOperator Functional Interface (0) | 2024.08.13 |
---|---|
BiFunction Functional Interface (0) | 2024.08.13 |
BiPredicate Functional Interface (0) | 2024.08.13 |
Predicate Functional Interface (0) | 2024.08.13 |
BiConsumer Functional Interface (0) | 2024.08.13 |