IT/JAVA
[JAVA/자바] 람다식(Lambda expression) 에 대해 알아보기
JSOpg
2018. 9. 10. 18:41
/* 함수적 인터페이스(@FunctionalInterface)
람다식이 하나의 메서드를 정의하기 때문에 두 개 이상의 추상 메서드를 가질 수 없다.
하나의 추상 메서드가 선언된 인터페이스를 함수적 인터페이스(functional interface)라고
한다.
@FunctionalInterface 어노테이션을 붙여 사용한다. */
/* Runnable interface => FunctionalInterface */
// 익명 중첩 클래스 내에서 람다식 사용이 가능하다.
public class Ex01Lambda {
public static void main(String[] args) {
// 기존 방식
// Thread t1 = new Thread(Runnable target)
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("기존 익명 중첩 클래스 방식");
}
});
t1.start();
// 람다식(JAVA 1.8~)
// Thread t3 = new Thread(()-> System.out.println("람다식 방식"));
Thread t2 = new Thread(()->{ // 한 줄이면 블럭({})을 쌓지 않아도 된다.
System.out.println("람다식 방식");
});
t2.start();
}
}