forked from winterbe/java8-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambda2.java
More file actions
64 lines (50 loc) · 2.49 KB
/
Lambda2.java
File metadata and controls
64 lines (50 loc) · 2.49 KB
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
55
56
57
58
59
60
61
62
63
64
package com.winterbe.java8.samples.lambda;
/**
* @author Benjamin Winterberg
*/
public class Lambda2 {
// 이 어노테이션은 개발자가 functional interface로 쓸 인터페이스에 두 개 이상의
// 추상 메서드를 선언했는지 컴파일러가 확인하게 함.
@FunctionalInterface
public static interface Converter<F, T> {
/*
각각의 람다는 인터페이스를 통해 정의할 수 있음(이전의 익명클래스->람다 표현식 예제를 보면 당연함)
람다 식을 보면 알지만 람다는 하나의 메서드만 정의함. 그래서 람다로 표현할 수 있는 인터페이스는 하나의
추상 메서드만 선언할 수 있음. 이런 인터페이스를 Functional Interface 라고 부름.
단, default method 는 람다 식으로 구현할 필요가 없기 때문에 functional interface 에 몇 개를 정의해도 상관 없음
*/
T convert(F from);
}
static class Something {
String startsWith(String s) {
return String.valueOf(s.charAt(0));
}
}
interface PersonFactory<P extends Person> {
P create(String firstName, String lastName);
}
public static void main(String[] args) {
Converter<String, Integer> integerConverter1 = (from) -> Integer.valueOf(from);
Integer converted1 = integerConverter1.convert("123");
System.out.println(converted1); // result: 123
// method reference
/*
"::" 키워드는 이미 있는 메서드를 참조할 때 쓰는 키워드이다.
*/
// 이전 Functional Interface 예제는 다음과 같이 줄어들 수 있다.
// static 인 경우, Class명::static메서드명
Converter<String, Integer> integerConverter2 = Integer::valueOf;
Integer converted2 = integerConverter2.convert("123");
System.out.println(converted2); // result: 123
// 멤버 메서드인 경우, 변수명::메서드명
Something something = new Something();
Converter<String, String> stringConverter = something::startsWith;
String converted3 = stringConverter.convert("Java");
System.out.println(converted3); // result J
// constructor reference
// 생성자에도 적용 가능, Class명::new
// 매개변수 형태가 일치하는 생성자를 호출함.
PersonFactory<Person> personFactory = Person::new;
Person person = personFactory.create("Peter", "Parker");
}
}