타겟 타입(target type)
람다식이 대입되는 인터페이스를 말한다. 인터페이스(타겟 타입) 변수 = 람다식;
익명 구현 객체를 만들 때 사용할 인터페이스이다.
함수적 인터페이스(functional interface)
모든 인터페이스는 람다식의 타겟 타입이 될 수 없다.
람다식은 하나의 메소드를 정의하기 떄문에..
나의 추상 메소드만 선언된 인터페이스만 타겟 타입이 될 수 있다.
함수적 인터페이스
하나의 추상 메소드만 선언된 인터페이스를 말한다.
@FunctionalInterface 어노테이션
하나의 추상 메소드만을 가지는지 컴파일러가 체크하도록 함
두 개 이상의 추상 메소드가 선언되어 있으면 컴파일 오류 발생
매개 변수와 리턴값이 없는 람다식
@FunctionalInterface public interface MyFunctionalInteface ( public void method(); } |
MyFunctionalInterface fi = () -> { ... } fi.method(); |
Exam
//하나의 추상 메소드만을 가지는지 컴파일러가 체크하도록 함
@FunctionalInterface // 두 개 이상의 추상 메소드가 선언되어 있으면 컴파일 오류 발생
public interface MyFunctionalInterface {
public void method();
// public void method2(); 주석을 해제하면 컴파일 오류 발생
}
public class MyFunctionalInterfaceExample {
public static void main(String[] args) {
MyFunctionalInterface fi;
fi = () -> {
String str = "method call1";
System.out.println(str);
};
fi.method(); //실행결과 method call1
fi = () -> { System.out.println("method call2");};
fi.method(); //실행결과 method call2
fi = () -> System.out.println("method call3"); //실행문이 하나 이므로 중괄호 생략
fi.method(); //실행결과 method call3
fi = new MyFunctionalInterface() {
@Override
public void method() {
System.out.println("method call4");
}
};
fi.method(); //실행결과 method call4
}
}
매개변수가 있는 람다식
@FunctionalInterFace public interface MyFunctionalInterface { public void method(int x); } |
MyFunctionalInterface fi = (x) -> { ... } 또는 x -> { ... } fi.method(5) |
Exam
//하나의 추상 메소드만을 가지는지 컴파일러가 체크하도록 함
@FunctionalInterface // 두 개 이상의 추상 메소드가 선언되어 있으면 컴파일 오류 발생
public interface MyFunctionalInterface {
public void method(int x); //매개변
// public void method2(); 주석을 해제하면 컴파일 오류 발생
}
public class MyFunctionalInterfaceExample {
public static void main(String[] args) {
MyFunctionalInterface fi;
fi = (x) -> {
int result = x * 5;
System.out.println(result);
};
fi.method(2); //실행 결과 10
fi = (x) -> System.out.println(x * 5); //실행문이 하나일 경우 중괄호 생략 {}
fi.method(2); //실행 결과 10
fi = x -> System.out.println(x * 5); //매개변후가 하나일 경우 괄호() 생략
fi.method(2); //실행 결과 10
}
}
리턴값이 있는 람다식
@FunctionalInterFace public interface MyFunctionalInterface { public int void method(int x, int y); } |
MyFunctionalInterFace fi = (x, y) -> { ...; return 값; } int result = fi.method(2, 5) |
MyFunctionalInterface fi = (x, y) ->{ return x + y; } |
MyFunctionalInterFace fi = (x, y) -> x + y; |
MyFunctionalInterFace fi = (x, y) -> { return sum(x, y); } |
MyFunctionalInterFace fi = (x, y) -> sum(x, y); |
Exam
//하나의 추상 메소드만을 가지는지 컴파일러가 체크하도록 함
@FunctionalInterface // 두 개 이상의 추상 메소드가 선언되어 있으면 컴파일 오류 발생
public interface MyFunctionalInterface {
public int method(int x, int y); //매개변수가 2개 int타입 리턴
// public void method2(); 주석을 해제하면 컴파일 오류 발생
}
public class MyFunctionalInterfaceExample {
public static void main(String[] args) {
MyFunctionalInterface fi;
fi = (x, y) -> {
int result = x + y;
return result;
};
System.out.println(fi.method(2,5)); //실행 결과 7
fi = (x, y) -> { return x + y; };
System.out.println(fi.method(2,5)); //실행 결과 7
fi = (x, y) -> x + y; //중괄호 블럭에 return문만 있을경우 중괄호 생략가능, return 생략가능
System.out.println(fi.method(2,5)); //실행 결과 7
//아래의 static int sum() 으로 실행
fi = (x, y) -> sum(x, y); // == {return sum(x, y);};
System.out.println(fi.method(2,5)); //실행 결과 7
}
public static int sum(int x, int y) {
return (x + y);
}
}