핵심정리 1: java.util.function 패키지

API가 다루는 개념의 수가 줄어들어 익히기 더 쉬워진다.

표준 함수형 인터페이스들은 유용한 디폴트 메서드를 많이 제공하므로 다른 코드와의 상호운용성도 크게 좋아진다.

image.png

핵심정리 2: 직접 정의해서 사용하는 경우

자주 쓰이며, 이름 자체가 용도를 명확히 설명해준다.

반드시 따라야 하는 규약이 있다.

유용한 디폴트 메서드를 제공할 수 있다.

@FunctionalInterface

• 그 인터페이스가 람다용으로 설계된 것임을 알려준다. • 해당 인터페이스가 추상 메서드를 오직 하나만 가지고 있어야 컴파일 되게 해준다. • 유지보수 과정에서 누군가 실수로 메서드를 추가하지 못하게 막아준다

@FunctionalInterface
public interface TriFunction<T, U, V, R> {
    R apply(T t, U u, V v);
}

===

public class TriFunctionExample {
    public static void main(String[] args) {
        // Using TriFunction to concatenate three strings
        TriFunction<String, String, String, String> concatenate = (s1, s2, s3) -> s1 + s2 + s3;

        String result = concatenate.apply("Hello, ", "world", "!");
        System.out.println(result); // Output: Hello, world!

        // Using TriFunction to sum three integers
        TriFunction<Integer, Integer, Integer, Integer> sum = (a, b, c) -> a + b + c;

        Integer sumResult = sum.apply(1, 2, 3);
        System.out.println(sumResult); // Output: 6
    }
}