Skip to content

Instantly share code, notes, and snippets.

@hongbeomi
Created November 26, 2019 08:01
Show Gist options
  • Save hongbeomi/1c7e0118ca22482d3a8f64344d8cd93b to your computer and use it in GitHub Desktop.
Save hongbeomi/1c7e0118ca22482d3a8f64344d8cd93b to your computer and use it in GitHub Desktop.
// 리스트 형식으로 추상화!
public interface Predicate<T> {
boolean test(T t);
}
public static <T> List<T> filter(List<T> list, Predicate<T> p) { // 형식 파라미터 T
List<T> result = new ArrayList<>();
for(T e: list) {
if(p.test(e)) {
result.add(e);
}
}
return result;
}
// 람다 표현식을 이용해서 필터 메서드를 사용해보기
List<Apple> redApples = filter(inventory, (Apple apple) -> RED.equals(apple.getColor()));
List<Integer> evenNumbers = filter(numbers, (Integer i) -> i % 2 == 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment