
함수(메서드)를 간단한 식(expression)'으로 표현하는 방법
int max(int a, int b) {
return a>b ? a : b ;
}
(a,b) -> a > b ? a : b



@FunctionalInterface //안 붙여도 되는데,
함수형 인터페이스는 단 하나의 추상 메서드만 가져야하니까 여기에 두개 메서드를 선언했을 때 에러가 나면서 잡아 줌
interface MyFunction {
public abstarct int max(int a, int b);
}
//public abstrat은 안 붙여도 됨. 인터페이스는 항상 public abstrat이니까.
MyFunction f = new MyFunction() {
public abstrat int max(int a, int b) {
return a > b ? a : b;
}
}
int value = f.max(3,5);
*오버라이딩 규칙 : 접근제한자는 더 좁게 못 바꾼다.
Myfunction f = (a,b) -> a>b ? a: b;
int value = f.(3,5);

List<String> list = Arrays.asList("abc", "aaa", "bbb", "ddd", "aaa");
Collection.sort(list, new Comparator<String>() {
public int compare(String s1, String s2) {
return s2.compareTo(s1);
}
});
↓
interface Comparator<T> {
int compare(T o1, T o2);
}
List<String> list = Arrays.asList("abc", "aaa", "bbb", "ddd", "aaa");
Collection.sort(list,(s1, s2) -> s2.compareTo(s1));

interface MyFuntion {
void myMethod();
}
void aMethod(MyFuntion f) {
f.myMethod(); //MyFuntion에 정의된 메서드 호출
}
람다식에 myMethod라는 이름을 붙여준 셈
MyFuntion f = () -> System.out.println("myMethod()");
a.Method(f);
↓
aMethod(() -> System.out.println("myMEethod()"));

와~~ 진짜 모르겠다 이거~~


매개변수 세 개여야하면? -> 직접 만들면 됨


and() or() negate()로 두 predicate를 하나로 결합.
negate()는 not(!)


연습문제:람다식을 두개로 연결할 수 있다는것만 알고 넘어가시면 될거같아요. 깊게 파지마시고요.

메서드 참조로 간단히 할 수 있다.클래스이름::메서드이름
Supplier<Myclass> s = () -> new Myclass();
생성만. 주기만 함. 입력이 없고 출력만 있음.
위 예시는 Myclass 타입의 객체만 주는 것.
어우 씨 몰라 이건 책 봐