하나의 메서드만 호출하는 람다식은 '메서드 참조'로 간단히 할 수 있다.

static 메서드 참조

람다식 -> 메서드 참조. 즉, 람다식을 더 간단하게
Supplier<MyClass> s = () -> new MyClass();
이것을(1)
Supplier<MyClass> s = MyClass::new;
이렇게
이것을(2)
Function<Integer, MyClass> s = (i) -> new MyClass(i);
이렇게
Function<Integer, MyClass> s = MyClass::new; // (i)값 이미 알고 있으므로 생략
Function<Integer, int[]> f1 = new int[x]; // 람다식
Function<Integer, int[]> f2 = int[]::new // 메서드 참조
class Java {
public static void main(String[] args) {
// Supplier는 입력x, 출력o
// Supplier<MyClass> s = () -> new MyClass();
// Supplier<MyClass> s = MyClass::new;
// Function<Integer, MyClass> f = (i) -> new MyClass(i);
Function<Integer, MyClass> f = MyClass::new;
MyClass mc = f.apply(100);
System.out.println(mc.iv);
System.out.println(f.apply(200).iv);
// Function<Integer, int[]> f2 = (i) -> new int[i];
Function<Integer, int[]> f2 = int[]::new; // 메서드 참조
int[] arr = f2.apply(100);
System.out.println("arr.length : "+arr.length);
}
}
class MyClass{
int iv;
MyClass(int iv){
this.iv = iv;
}
}