Created
November 26, 2019 08:01
-
-
Save hongbeomi/7fc2d66843040c3ff09d4afc7b0aad19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 리스트 형식으로 추상화! | |
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