Programming/Java
[Java] - 3. 자바에서 제공하는 함수형 인터페이스
rw-
2023. 4. 1. 16:46
728x90
3. 자바에서 제공하는 함수형 인터페이스
Java가 기본으로 제공하는 함수형 인터페이스
- java.util.function 패키지
- 자바에서 미리 정의해둔 자주 사용할만한 함수 인터페이스
- Function<T, R>
- BiFunction<T, U, R>
- Consumer
- Supplier
- Predicate
- UnaryOperator
- BinaryOperator
Function<T, R>
- T 타입을 받아서 R 타입을 리턴하는 함수 인터페이스
- R apply(T t)
- 함수 조합용 메소드
- andThen
- compose
Function<Integer, Integer> plus10_1 = (i) -> i + 10;
Function<Integer, Integer> multiply2 = (i) -> i * 2;
System.out.println("plus10_1.apply(1): " + plus10_1.apply(1));
System.out.println("multiply2.apply(2): " + multiply2.apply(2));
System.out.println("plus10_1.compose(multiply2).apply(2): " + plus10_1.compose(multiply2).apply(2));
System.out.println("plus10_1.andThen(multiply2).apply(2): " + plus10_1.andThen(multiply2).apply(2));
plus10_1.apply(1): 11
multiply2.apply(2): 4
plus10_1.compose(multiply2).apply(2): 14
plus10_1.andThen(multiply2).apply(2): 24
BiFunction<T, U, R>
- 두 개의 값(T, U)를 받아서 R 타입을 리턴하는 함수 인터페이스
- R apply(T t, U u)
Consumer
- T 타입을 받아서 아무값도 리턴하지 않는 함수 인터페이스
- void Accept(T t)
- 함수 조합용 메소드
- andThen
Consumer<Integer> printT = (i) -> System.out.println(i);
printT.accept(10);
10
Supplier
- T 타입의 값을 제공하는 함수 인터페이스
- T get()
Supplier<Integer> get10 = () -> 10;
System.out.println("get10: " + get10);
System.out.println("get10.get(): " + get10.get());
get10: me.whiteship.java8to11.FooPlus10$$Lambda$21/0x0000000800c03480@7699a589
get10.get(): 10
Predicate
- T 타입을 받아서 boolean을 리턴하는 함수 인터페이스
- boolean test(T t)
- 함수 조합용 메소드
- And
- Or
- Negate
UnaryOperator
- Function<T, R>의 특수한 형태로, 입력값 하나를 받아서 동일한 타입을 리턴하는 함수 인터페이스
BinaryOperator
- BiFunction<T, U, R>의 특수한 형태로, 동일한 타입의 입렵값 두개를 받아 리턴하는 함수 인터페이스
https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html
728x90
반응형