Chapter04. 리액티브 프로그래밍을 위한 사전 지식

김신영·2023년 7월 30일
0

Spring WebFlux

목록 보기
4/13
post-thumbnail

함수형 인터페이스 (Functional Interface)

단 하나의 추상 메서드만 있는 인터페이스

@FuntionalInterface
public interface Comparator<T> {
	int compare(T o1, T o2);

	// ...
}

람다 표현식 (Lambda Expression)

(String a, String b) -> a.equals(b)

함수형 인터페이스를 구현한 클래스의 인스턴스를 람다 표현식으로 작성해서 전달한다.

  • 람다 표현식에서는 파라미터로 전달받은 변수 뿐 아니라, 람다 표현식 외부에서 정의된 변수 (Free Variable) 역시 사용할 수 있다.
  • ⚠️ 람다 표현식에서 사용되는 자유 변수는 final 또는 final 같은 효력을 지녀야 한다!

람다 캡처링

  • 람다 표현식 외부에서 정의된 자유 변수를 람다 표현식에 사용하는 것!

메서드 레퍼런스 (Method Reference)

(Car car) -> car.getCarName()

Car::getCarName
  • ClassName :: static method
  • ClassName :: instance method
  • object :: instance method
  • ClassName :: new

함수 디스크립터 (Function Descriptor)

Functional InterfaceFunction Descriptor
Predicate<T>T → boolean
Consumer<T>T → void
Function<T, R>T → R
Supplier<T>() → T
BiPredicate<L, R>(L, R) → boolean
BiConsumer<T, U>(T, U) → void
BiFunction<T, U, R>(T, U) → R

Functional InterfaceFunction DescriptorSpecific Interface
Predicate<T>T -> booleanIntPredicate
LongPredicate
DoublePredicate
Consumer<T>T -> voidIntConsumer
LongConsumer
DoubleConsumer
Function<T, R>T -> RIntFunction<R>
IntToLongFunction
IntToDoubleFunction
LongFunction<R>
LongToIntFunction
LongToDoubleFunction
DoubleFunction<R>
DoubleToIntFunction
DoubleToLongFunction
ToIntFunction<T>
ToLongFunction<T>
ToDoubleFunction<T>
Supplier<T>() -> TBooleanSupplier
IntSupplier
LongSupplier
DoubleSupplier
UnaryOperator<T>T -> TIntUnaryOperator
LongUnaryOperator
DoubleUnaryOperator
BinaryOperator<T>(T, T) -> TIntBinaryOperator
LongBinaryOperator
DoubleBinaryOperator
BiPredicate<T, U>(T, U) -> boolean
BiConsumer<T, U>(T, U) -> voidObjIntConsumer<T>
ObjLongConsumer<T>
ObjDoubleConsumer<T>
BiFunction<T, U, R>(T, U) -> RToIntBiFunction<T, U>
ToLongBiFunction<T, U>
ToDoubleBiFunction<T, U>
profile
Hello velog!

0개의 댓글