람다와 클로저
lambda와 closure에 대해서 알아보자
Last updated
(Apple a) -> a.getWeight() > 150public class Store {
private String storeNo = "9000";
public void lambdaClosure() {
Function<String,Integer> lambdaFunction = i -> {
// lambda 내에서 Store 클래스 멤버 변수인 storeNo를 사용!!
System.out.println(this.storeNo);
return null;
};
}
}@since Java 1.1!
void anonymousClassClosure() {
Server server = new HttpServer();
waitFor(new Condition() {
@Override
public Boolean isSatisfied() {
return !server.isRunning();
}
});
}public class Store {
private String storeNo = "9000";
public void anonymousClosure() {
String anonymousNo = "1000";
Function<String, Integer> anonymousFunction = new Function<String, Integer>() {
@Override
public Integer apply(String s) {
// Store class의 storeNo를 직접 접근할 수 있다.
System.out.println(Store.this.storeNo);
return Integer.parseInt(anonymousNo);
}
};
}
}