package ex07.ch02;
/*
람다표현식의 다섯가지
1. Consumer
2. Supplier
3. Predicate (true or false) 논리
4. Function
5. Callable
*/
//소비하는 친구
interface Con01 {
void accept(int n);
}
//공급하는 친구
interface Sup01 {
int get();
// 누군가가 호출해서 받을 때 써서 보통 get으로 씀
// 타입은 달라질 수 있음 다만 리턴값이 있어야한다는 사실을 알 것.
}
//예견하는 친구
interface Pre01 {
boolean test(int n);
}
//함수
interface Fun01 {
int apply(int n1, int n2);
}
//기본
interface Cal01 {
void call();
}
public class Beh02 {
public static void main(String[] args) {
// 1. Consummer
Con01 c1 = (int n) -> {
System.out.println("소비함 : " + n);
};
c1.accept(10);
// 2. Supplier 중괄호에서 return값을 받는 것보다 생략하고 리턴값을 바로 넣어 축약할 수 있음.
Sup01 s1 = () -> 1;
int r1 = s1.get();
System.out.println("공급받음 : " + r1);
// 3. Predicate(논리) 내나 리턴이라 위와같이 중괄호 생략가능
Pre01 p1 = (n) -> n % 2 == 0;
Pre01 p2 = (n) -> n % 3 == 0;
System.out.println("예측함 : " + p1.test(5));
System.out.println("예측함 : " + p2.test(6));
// 4. Function
Fun01 add = (int n1, int n2) -> n1 + n2;
Fun01 sub = (int n1, int n2) -> n1 - n2;
Fun01 mul = (int n1, int n2) -> n1 * n2;
Fun01 div = (int n1, int n2) -> n1 / n2;
System.out.println("더하기 함수 : " + add.apply(1, 2));
System.out.println("빼기 함수 : " + sub.apply(3, 4));
System.out.println("곱하기 함수 : " + mul.apply(5, 6));
// 5. Callable {
}
}
Share article