티스토리 뷰

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

  • 단 하나의 추상 메서드만 정의된 인터페이스
  • 함수를 일급시민(값)으로 취급
    • 함수 자체를 파라미터로 전달 가능
@FunctionalInterface
public interface Comparator<T> {
    int compare(T o1, T o2); // 단 하나의 추상 메서드
    ...
    default Comparator<T> reversed() {
        return Collections.reverseOrder(this);
    }

    default Comparator<T> thenComparing(Comparator<? super T> other) {
        Objects.requireNonNull(other);
        return (Comparator<T> & Serializable) (c1, c2) -> {
            int res = compare(c1, c2);
            return (res != 0) ? res : other.compare(c1, c2);
        };
    }
    ...
}

4.2 람다 표현식(Lambda Expression)

  • 함수형 인터페이스를 구현한 클래스의 메서드 구현을 단순화한 표현식

4.3 메서드 레퍼런스(Method Reference)

  • 람다 표현식을 더 간결하게 작성하는 방법
    • ClassName :: static method 유형
    • ClassName :: instance method 유형
    • object :: instance method 유형
    • ClassName :: new 유형
// 람다 표현식
(Car car) -> car.getCarName()
// 메서드 레퍼런스
Car::getCarName

4.4 함수 디스크립터(Function Descriptor) 

  • 함수형 인터페이스의 파라미터 형식과 리턴 값의 형태를 설명하는 것

댓글