함수형 인터페이스는 가능하면 스페셜한 놈을 찾아써라...
Core Java SE 9 For the impatient (가장 빨리 만나는 코어자바9)책에도 한 페이지지만 분명 언급되어 있었다. (158쪽)
기본 타입 int, long, double에 특화된 함수형 인터페이스 34개...
이것을 사용을 하면 오토박싱을 줄일 수 있어서 성능에 향상을 줄 수 있다.
| Current Interface | Preferred Interface |
|---|---|
| Function<Integer, R> | IntFunction<R> |
| Function<Long, R> | LongFunction<R> |
| Function<Double, R> | DoubleFunction<R> |
| Function<Double,Integer> | DoubleToIntFunction |
| Function<Double,Long> | DoubleToLongFunction |
| Function<Long,Double> | LongToDoubleFunction |
| Function<Long,Integer> | LongToIntFunction |
| Function<R,Integer> | ToIntFunction<R> |
| Function<R,Long> | ToLongFunction<R> |
| Function<R,Double> | ToDoubleFunction<R> |
| Function<T,T> | UnaryOperator<T> |
| BiFunction<T,T,T> | BinaryOperator<T> |
| Consumer<Integer> | IntConsumer |
| Consumer<Double> | DoubleConsumer |
| Consumer<Long> | LongConsumer |
| BiConsumer<T,Integer> | ObjIntConsumer<T> |
| BiConsumer<T,Long> | ObjLongConsumer<T> |
| BiConsumer<T,Double> | ObjDoubleConsumer<T> |
| Predicate<Integer> | IntPredicate |
| Predicate<Double> | DoublePredicate |
| Predicate<Long> | LongPredicate |
| Supplier<Integer> | IntSupplier |
| Supplier<Double> | DoubleSupplier |
| Supplier<Long> | LongSupplier |
| Supplier<Boolean> | BooleanSupplier |
| UnaryOperator<Integer> | IntUnaryOperator |
| UnaryOperator<Double> | DoubleUnaryOperator |
| UnaryOperator<Long> | LongUnaryOperator |
| BinaryOperator<Integer> | IntBinaryOperator |
| BinaryOperator<Long> | LongBinaryOperator |
| BinaryOperator<Double> | DoubleBinaryOperator |
| Function<T, Boolean> | Predicate<T> |
| BiFunction<T,U,Boolean> | BiPredicate<T,U> |
따라서 이전에 쓰였던 https://namocom.tistory.com/771의 경우 성능하고는 상관은 의미상으로 UnaryOperator로 바꾸라는 것 같다.
사실, UnaryOperator는 Function 인터페이스를 상속한다.

Function<String, String> transformer
를
UnaryOperator<String> transformer
로 바꾸면 된다.
프랙티스
몇 번 경험을 해보니
Function<T, R> 의 경우 입력과 반환이 같은 경우에는 (Function<T, T>) UnarayOperator<T>로 바꿀 수 있고,
반환타입이 필요없는 UnaryOperator<T>의 경우에는 Consumer<T>로 바꿀 수 있었다.
(위에서 반환타입이 필요없는 이유는 입력으로 받는 인자를 그대로 return해주는 경우였다.)
'Programing > JVM(Java, Kotlin)' 카테고리의 다른 글
| [Java] Effective Java : Item 36 EnumSet, 유스케이스 in Java 8 (0) | 2019.10.27 |
|---|---|
| [Java] Stream in depth : Stream & Pipeline (0) | 2019.10.27 |
| [javac] 자바 컴파일러: 컴파일 단계 (0) | 2019.10.12 |
| 성지순례와 인터페이스 (0) | 2019.10.08 |
| [Java] Wrapper 클래스 vs Overload 메서드 중 우선순위는? (1) | 2019.10.06 |