함수형 프로그래밍에서 함수는 1급 객체로 취급받기 때문에 위의 예제에서 본 것 처럼 함수를 파라미터로 넘기는 등의 작업이 가능한 것이다. 또한 우리가 일반적으로 알고 개발했던 함수들은 함수형 프로그래밍에서 정의하는 순수 함수들과는 다르다는 것을 인지해야 한다.
선언방법
public void print() {
String s = "test";
System.out.println(s);
() -> {
String s = "test";
System.out.println(s);
public void print(String s) {
System.out.println(s);
}
s -> System.out.println(s);
public int add(int x, int y) {
return x + y;
}
(x, y) -> x + y
//구현해야 할 메소드가 한개이므로 Functional Interface이다.
@FunctionalInterface
public interface Math {
public int Calc(int first, int second);
}
//구현해야 할 메소드가 두개이므로 Functional Interface가 아니다. (오류 사항)
@FunctionalInterface
public interface Math {
public int Calc(int first, int second);
public int Calc2(int first, int second);
}
함수형 인터페이스 람다 사용예제
함수형 Interface 선언
@FunctionalInterface
interface Math {
public int Calc(int first, int second);
}
추상 메소드 구현 및 함수형 인터페이스 사용
public static void main(String[] args){
Math plusLambda = (first, second) -> first + second;
System.out.println(plusLambda.Calc(4, 2));
Math minusLambda = (first, second) -> first - second;
System.out.println(minusLambda.Calc(4, 2));
}
@FunctionalInterface
public interface Convertible {
void convert(int USD);
}
public class KRWConverter implements Convertible {
@Override
public void convert(int USD) {
System.out.println(USD + " 달러 = " + (USD * 1400) + "원");
}
}
public interface Calculator{
public int cal(int num1,int num2);
}
1) 매개변수가 없고, 리턴값이 없는 람다식
@FunctionalInterface
public interface ConvertibleWithNoParams {
void convert();
}
public class _04_FunctionalInterface {
public static void main(String[] args) {
ConvertibleWithNoParams c1 = () -> System.out.println("1달러는 1400원");
c1.convert();
}
}
2) 매개변수가 있고, 리턴값이 없는 람다식
@FunctionalInterface
public interface ConvertibleWithTwoParams {
void convert(int USD, int KRW);
}
public class _04_FunctionalInterface {
public static void main(String[] args) {
ConvertibleWithTwoParams c2 = (d, w) -> System.out.println(d + " 달러 = " + (d * w) + "원");
c2.convert(10, 1500);
}
}
3) 매개변수가 있고, 리턴값이 있는 람다식
@FunctionalInterface
public interface ConvertibleWithReturn {
int convert(int USD, int KRW);
}
public class _04_FunctionalInterface {
public static void main(String[] args) {
ConvertibleWithReturn c3 = (d, w) -> {return d * w};
int res = c3.convert(20, 1400);
System.out.println(res + "원");
}
}
}
4) 매개변수가 없고, 리턴값이 있는 람다식
@FunctionalInterface
public interface JavaCoding {
String nowCoding();
}
public class Execute {
public static void main(String[] args) {
//객체 선언
JavaCoding jc;
String str1 = "그 날을 잊지 못해 baby";
String str2 = "날 보며 환히 웃던 너의 미소에";
String str3 = "홀린 듯 I'm fall in love";
jc = () -> {
return str1;
};
System.out.println(jc.nowCoding());
jc = () -> { return str2; };
System.out.println(jc.nowCoding());
//실행코드가 return 만 있는 경우 {}와 return문 생략가능
jc = () -> str3;
System.out.println(jc.nowCoding());
}
}
참고
나도코딩 유튜브
https://mangkyu.tistory.com/113
https://khj93.tistory.com/entry/JAVA-%EB%9E%8C%EB%8B%A4%EC%8B%9DRambda%EB%9E%80-%EB%AC%B4%EC%97%87%EC%9D%B4%EA%B3%A0-%EC%82%AC%EC%9A%A9%EB%B2%95