Lambda(람다), 익명클래스 표현식
람다는 java 뿐만 아니라 javascript에서도 사용된다.
Function<String, Integer> toInt = new Function<String, Integer>(){
@Override
public Integer apply(String value){
return Integer.parseInt(value);
}
};
int result = toInt.apply("100");
System.out.println(result);
위 toInt라는 익명클래스를 간소화하여 표현할 수 있다.
Function<String, Integer> toInt = (value)->Integer.parseInt(value); //Lambda Expression
int result = toInt.apply("123");
System.out.println(result);
위 소스 중 (value)는 함수 파라미터이며, 한개일 경우 괄호를 생략할 수 있다.
여러개라면 (value1, value2)의 형태로 전달 가능.
처음에 쓰기 어렵지만 잘 쓰기만 한다면, 가독성이 더 좋아진다.
'Java' 카테고리의 다른 글
JPA - ORM 튜토리얼 (0) | 2017.11.01 |
---|---|
java8 functionalInterface Function (5) | 2017.01.10 |
String StringBuffer StringBuilder 차이 (2) | 2016.12.27 |
Spring DI와 IoC [ Dependency Injection + Inversion of Control ] 요약 (0) | 2016.12.23 |
엔티티 생명주기 (0) | 2016.12.21 |