[JAVA8] 자바에서 제공하는 함수형 인터페이스

Java가 기본으로 제공하는 함수형 인터페이스

  • Java.lang.function 패키지
  • 자바에서 미리 정의해둔 자주 사용할만한 함수 인터페이스

 

  • Function<T , R>
    • 항상 입력 인수를 반환하는 함수를 반환
      • (static) identity
    • T 타입을 받아서 R 타입을 리턴하는 함수 인터페이스
      • R  apply(T t)
    • 함수 조합용 메서드
      • (default) andThen
      • (default) compose
public class FunctionTest implements Function<Integer , Integer> {
    @Override
    public Integer apply(Integer integer) {
        return integer + 100;
    }
}
    public static void main(String[] args) {

        // 객체 생성
        FunctionTest test1 = new FunctionTest();
        System.out.println("객체 생성 : " + test1.apply(10));

        // 람다 표현식
        Function<Integer , Integer> add = (i) -> i + 10;
        System.out.println("람다 표현식 (더하기) : " + add.apply(10));
        Function<Integer , Integer> multi = (i) -> i * 20;
        System.out.println("람다 표현식 (곱하기) : " + multi.apply(10));

        // 곱하기를 수행한 후 더하기를 수행한 값을 리턴한다.
        // 10 * 20 + 10
        Function<Integer , Integer> compose = add.compose(multi);
        System.out.println("람다 표현식 (compose) : " + compose.apply(10));

        // 더하기를 수행 한 후 곱하기를 수행한 값을 리턴한다.
        // 10 + 10 * 20
        Function<Integer , Integer> andThen = add.andThen(multi);
        System.out.println("람다 표현식 (andThen) : " + andThen.apply(10));
        
//        출력
//        객체 생성 : 110
//        람다 표현식 (더하기) : 20
//        람다 표현식 (곱하기) : 200
//        람다 표현식 (compose) : 210
//        람다 표현식 (andThen) : 400
    }

 

  • BiFunction<T , U , R>
    • 두 개의 값(T , U)를 받아 R 타입을 리턴하는 함수 인터페이스
      • R apply(T t , U u)
    • 함수 조합용 메서드
      • (default) andThen
        • return (T t, U u) -> after.apply(apply(t, u));
  • Consumer<T>
    • T 타입을 받아서 아무 값도 리턴하지 않는 함수 인터페이스
      • void accept(T t)
    • 함수 조합용 메서드
      • (default) andThen
  • Supplier<T>
    • T 타입의 값을 제공하는 함수 인터페이스
      • T get()
    public static void main(String[] args) {
        Supplier<Integer> get = () -> 1;
        System.out.println(get.get());

        // 출력
        // 1
    }

 

  • Predicate<T>
    • T 타입을 받아서 boolean을 리턴하는 함수 인터페이스
      • boolean test(T t)
    • 함수 조합용 메서드
      • And
      • Or
      • Negate (Not)
    public static void main(String[] args) {
        Predicate<String> startsWithTis = (s) -> s.startsWith("t");
        Predicate<Integer> isEven = (s) -> s%2 == 0;
        System.out.println("startsWithTis : " + startsWithTis.test("istory"));
        System.out.println("isEven : " + isEven.test(10));
        System.out.println("isEven (negate) : " + isEven.negate().test(10));
//        출력
//        startsWithTis : false
//        isEven : true
//        isEven (negate) : false
    }

 

  • UnaryOperator<T> extends Function<T , T>
    • Function<T , R> 의 특수한 형태로 , 입력 값 하나를 받아서 동일한 타입을 리턴하는 함수 인터페이스
    public static void main(String[] args) {
        UnaryOperator<Integer> add = (i) -> i + 10;
        System.out.println("UnaryOperator (더하기) : " + add.apply(10));
        UnaryOperator<Integer> multi = (i) -> i * 20;
        System.out.println("UnaryOperator (곱하기) : " + multi.apply(10));
        
//        출력
//        UnaryOperator (더하기) : 20
//        UnaryOperator (곱하기) : 200
    }

 

  • BinaryOperator<T> extends BiFunction<T , T , T>
    • BiFunction<T , U , R>의 특수한 형태로 , 동일한 타입의 입력 값 두 개를 받아 리턴하는 함수 인터페이스
    • BiFunction 3개의 인자 값이 다 같다면 한 개로 줄여서 사용할 수 있다.

 

+

<? extends T>
T와 그 자손 타입만 가능(upper bound)

<? super T>
T와 그 조상 타입만 가능(lower bound)

<?>
제한 없이 모든 타입이 가능. <? extneds Object>와 동일한 표현

 

 

 

 

더 자바, Java 8 - 인프런

자바 8에 추가된 기능들은 자바가 제공하는 API는 물론이고 스프링 같은 제 3의 라이브러리 및 프레임워크에서도 널리 사용되고 있습니다. 이 시대의 자바 개발자라면 반드시 알아야 합니다. 이

www.inflearn.com