Java/Java 기타

자바에서 @FunctionalInterface 사용하는 한 가지 방법

kimc 2022. 5. 21. 10:55

```

자바에서 @FunctionalInterface 사용하는 한 가지 방법

```

이번 글을 통해 배워갈 내용

  1. @FunctionalInterface 정의
  2. @FunctionalInterface Annotation 예시

 


 

Annotation이란

https://codemasterkimc.tistory.com/397

 

자바 Annotation의 정의와 종류 그리고 사용 예시

``` Annotation의 정의 Annotation의 종류 Annotation의 사용 예시 ``` 이번 글을 통해 배워갈 내용  Annotation의 정의  Annotation의 종류  Annotation의 사용 예시 Annotation의 정의 Annotation은 메타데..

codemasterkimc.tistory.com

 

@FunctionalInterface 정의

Java 1.8부터 지원되는 기능으로서 

1개의 추상 메서드를 가진 인터페이스 (SAM Single Abstract Interface)를 함수형 인터페이스라고 합니다.

이러한 함수형 인터페이스는 자바 람다식에서 접근할 때 사용되는데 이를 컴파일러에게 함수형 인터페이스라고 알려서 컴파일 시 조건에 부합하지 않으면 에러를 출력하게 합니다.

 

 

@FunctionalInterface Annotation 예시

 

저는 자바에서 기본적으로 제공되는 함수형 인터페이스들만 사용하는 편이라서

직접 함수형 인터페이스를 만드는 일은 거의 없었지만 직접 만들수도 있습니다

 

Cat Functional interface

@FunctionalInterface
public interface CatFunctionalInterface<T>{
    T hey();
}

 

Interface 구현

public class App {
    public static void main(String[] args) {
        CatFunctionalInterface<String> impl = () -> "hey";
        System.out.print(impl.hey());
    }
}

hey 출력

 

기타

Predicate, Supplier, Callable, Consumer, Comparator, Runnable 등 정말 다양한 종류의 Functional Interface들이 있고

 

함수형 인터페이스에 대해서 추가로 알아보고자 하시는 분들은 아래 링크를 참조해주시면 됩니다.

 

 

참조

https://www.baeldung.com/java-8-functional-interfaces

 

Functional Interfaces in Java 8 | Baeldung

Quick and practical guide to Functional Interfaces present in Java 8.

www.baeldung.com

https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html

 

FunctionalInterface (Java Platform SE 8 )

An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method. Since default m

docs.oracle.com

https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

 

java.util.function (Java Platform SE 8 )

Interface Summary  Interface Description BiConsumer Represents an operation that accepts two input arguments and returns no result. BiFunction Represents a function that accepts two arguments and produces a result. BinaryOperator Represents an operation u

docs.oracle.com

 

읽어주셔서 감사합니다

 

무엇인가 얻어가셨기를 바라며

 

오늘도 즐거운 코딩 하시길 바랍니다 ~ :)

 


 

728x90