interface G1 {
<E extends Exception> Object m() throws E;
}
interface G2 {
<E extends Exception> String m() throws Exception;
}
@FunctionalInterface
interface G extends G1, G2 {}
class GG implements G {
@Override
public <E extends Exception> String m() throws E {
return null;
}
}
참고(정훈님 블로그)
userList.forEach(user -> System.out.println(user)); 이 코드의 콜스택을 보면 ArrayList.forEach -> Consumer.accept -> System.out.println 이다. 그런데, Consumer.accept 는 사실 필요없고, depth만 깊어지게 한다. 메소드 레퍼런스를 사용하면 이런 부분을 해결할 수 있다. userList.forEach(System.out::println);
https://sjh836.tistory.com/173?category=679845
결론
메서드 참조는 람다의 간단명료한 대안이 될 수 있다.
메서드 참조 쪽이 짧고 명확하다면 메서드 참조를 쓰고, 그렇지 않을 때만 람다를 사용하라.
람다보다는 메서드 참조를 사용하라
요약
메서드 참조 (Method Reference)
ClassName::methodName 형식으로 메서드 참조를 생성한다.
메서드 참조 (Method Reference)를 이용하면 기존 메서드 정의를 재활용해, 람다 표현식보다 더 가독성이 좋으며 자연스러울 수 있다.
Map.merge()란?
Method Reference vs. Lambda Expression
때로는 람다 표현식이 간단할 때가 있다. 코드 재사용성, 클래스 및 메소드명 길이, 메서드 로직의 단순한 정도를 고려하여 가독성이 높은 쪽으로 결정하자.
() -> action()
이ClassNameSoooooLong::action
보다 간결하다.x -> x
이Function.identity()
보다 간결하다.메서드 참조 유형 & 인자 전달
ClassName::methodName 기본 형식을 따라간다. 생성자는 methodName으로
new
를 이용한다.메서드 참조는 뒤에 parenthese가 생략되므로 인자 전달 방식이 헷갈릴 수 있다. (사실 제가 ㅎㅎ..) 다음 두 가지만 기억하자.
::
앞에 Class의 instance가 전달되지 않았을 때! 전달된 인자들 중 '첫 번째' 인자가 Class의 instance로 들어간다.Generic Lambda Expression
그런 건 없다. https://stackoverflow.com/questions/22588518/lambda-expression-and-generic-defined-only-in-method#answer-22588738 하지만 Generic Method Reference는 일부 가능하니 참고하자.
참고(정훈님 블로그)
결론
메서드 참조는 람다의 간단명료한 대안이 될 수 있다. 메서드 참조 쪽이 짧고 명확하다면 메서드 참조를 쓰고, 그렇지 않을 때만 람다를 사용하라.