01.기초
동적 파라미터화 코드 전달하기
public static List<Apple> filterGreenApples(List<Apple> inventory) {
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory) {
if ("green".equals(apple.getColor())) {
result.add(apple);
}
}
return result;
}public interface Predicate<T> {
boolean test(T t);
}
public class weightPredicate implements Predicate {
@Override
public boolean test(T t) {
return t.getWeight() > 150;
}
}
public class colorPredicate implements Predicate {
@Override
public boolean test(T t) {
return t.getColor() == Color.GREEN;
}
}
public <T> List<T> filter(List<T> list, Predicate<T> p) {
List<T> result = new ArrayList<>();
for (T e : list) {
if (p.test(e)) {
result.add(e);
}
}
return result;
}
//
List<Apple> greenApples = filter(inventory, new colorPredicate());람다 표현식
함수형 인터페이스
람다, 메서드 참조 활용
람다 표현식 조합 유용 메서드
Last updated