jdk 1.8 이상부터 지원.

함수형 프로그래밍을 가능케 함.

1회용 익명 클래스가 필요할 때 람다를 사용하여 코드를 줄일 수 있음.

 

[기본 문법 1 : 인수가 없는 경우]

접근제한자, 리턴타입 생략 가능

public void method(){
}

() -> {
}

* @FunctionalInterface : 추상메소드가 한개만 존재하는 인터페이스

* lambda는 Functional Interface 에만 사용이 가능

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package Java.Lamda.basic;
 
public class Main1 {
        
    public static void main(String[] args) {
 
        //기존방법대로 익명클래스를 사용한 구현
        Interface i = new Interface() {
            @Override
            public void sayHello() {
                System.out.println("HELLO! in anonymous class");
            }
        };
        i.sayHello();
        
        //lambda를 사용한 익명클래스 구현        
        //lambda 는 Functional Interface 에만 사용이 가능
        Interface lamda = () -> {
            System.out.println("HELLO! in anonymous method(lambda)");
        };
        lamda.sayHello();
    }
    
    //@FunctionalInterface 는 abstract method 가 오직 1개여야 한다.
    @FunctionalInterface
    interface Interface {
        void sayHello();
    }
}
 
cs

[결과]

HELLO! in anonymous class 
HELLO! in anonymous method(lambda)

 

[기본 문법 2 : 인수가 존재하는 경우]

인수 타입 생략이 가능.

return 생략 가능(함수 내 처리가 return 문이 전부인 경우)

public void method(int x, int y){
   return x+y;
}

(x, y) -> x+y;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package Java.Lamda.basic;
 
public class Main3 {
    
    public static void main(String[] args) {
        
        Calculation add = (x, y) -> {
            return x+y;
        };
        System.out.println(calculate(add23));
        
        
        Calculation multiply = (x, y) -> x*y;
        System.out.println(calculate(multiply, 23));
    }
    
    static Integer calculate(Calculation operation, Integer x, Integer y) {
        return operation.apply(x, y);
    }
    
    @FunctionalInterface
    interface Calculation {
        Integer apply(Integer x, Integer y);
    }
    
    class Calculator {
        Integer add(Calculation cal, Integer x, Integer y) {
            return cal.apply(x, y);
        }
    }
}

 

 

cs

[결과]

5
6

 

[기본 문법 3 : static 메소드, default 메소드]

jdk 1.8 부터 interface 에서 static 메소드, default 메소드 사용이 가능.

static 메소드 : 오버라이딩 불가
default 메소드 : 오버라이딩 가능

인터페이스에 메소드라니?!

추상메소드만 가질 수 있는게 아니라니?!

인터페이스가 메소드(default 메소드)를 가질 수 있게 되면서, 추상클래스와 인터페이스가 매우 비슷해졌다.

(인터페이스를 만들 때, 공통 관심사는 default 메소드로, 나머지 메소드를 abstract 메소드로 선언하면 기존의 추상클래스와 매우 비슷하다)

자바에서도 결국 다중상속이 가능하게 된 셈이다.

그렇다면 추상클래스와 인터페이스의 차이점은 이제 더이상 없는건가?

아직 아래와 같은 차이점이 존재한다.

추상클래스 : 다중상속이 불가, 인스턴스 변수를 가질 수 있다
인터페이스 : 다중구현이 가능, 인스턴스 변수를 가질 수 없다

Calculation : interface

ExtendsCalculation : Calculation interface 상속받은 interface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package Java.Lamda.basic;
 
public class Main4 {
 
    public static void main(String[] args) {
 
        Calculation add = (x, y) -> {
            return x+y;
        };
        add.print(23);
        Calculation.print2(23);
        System.out.println(calculate(add23));
        
        
        ExtendsCalculation multiply = (x, y) -> x*y; 
        multiply.print(23);
        System.out.println(calculate(multiply, 23));
        
    }
    
    static Integer calculate(Calculation operation, Integer x, Integer y) {
        return operation.apply(x, y);
    }
    
    @FunctionalInterface
    interface Calculation {
        Integer apply(Integer x, Integer y);
        
        default void print(Integer x, Integer y) {
            System.out.println("x : " + x);
            System.out.println("y : " + y);
        }
        
        static void print2(Integer x, Integer y) {
            System.out.println("x : " + x + ", y : " + y);
        }
    }
    
    class Calculator {
        Integer add(Calculation cal, Integer x, Integer y) {
            return cal.apply(x, y);
        }
    }
    
    @FunctionalInterface
    interface ExtendsCalculation extends Calculation{
        @Override
        default void print(Integer x, Integer y){
            System.out.println("x,y : " + x +","+ y);
        }
    }
    
}
 
cs

[결과]

x : 2
y : 3
x : 2, y : 3
5
x,y : 2,3
6

※ stackoverflow 및 각종 개발자 커뮤니티 QnA 에 Comparator Interface 구현시 람다를 어떻게 사용할 수 있냐는 질문이 꽤 많이 보인다.

1.8 에서의 Comparator 인터페이스를 들여다 보면, default 메소드 및 static 메소드 다수 존재하나 추상메소드는 오직 compare()메소드, 단 한 개 존재한다.

 

[메소드 참조]

https://developyo.tistory.com/193?category=688588

 

 

참고:

https://futurecreator.github.io/2018/07/20/java-lambda-type-inference-functional-interface/

 

반응형

'back > java' 카테고리의 다른 글

[Java] java reflection  (0) 2020.02.20
[Java] lambda 람다 2 (메소드참조)  (0) 2020.01.17
[Java] Generic 제네릭  (0) 2019.12.28
[Java] Compile  (0) 2019.12.22
[Java] Collection Framework  (0) 2019.12.21

+ Recent posts