728x90
- 모던 자바 인 액션 공부하다 살펴 봄.
- 프레디케이트(predicate)란 무엇인지 간단히 알아봄.
- 함수형 인터페이스로 입력된 값을 확인하여 false, true 를 반한하는 메소드 test()를 가지고 있음
public class Ch01 {
public static boolean isGreenApple(Apple apple) {
return "green".equals(apple.getColor());
}
public static boolean isHeavyApple(Apple apple) {
return apple.getWeight() > 150;
}
public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) {
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory) {
if (p.test(apple)) result.add(apple);
}
return result;
}
public static void main(String[] args) {
List<Apple> inventory = Arrays.asList(
new Apple(80, "green"),
new Apple(155, "green"),
new Apple(120, "red")
);
//[Apple{color='green', weight=80}, Apple{color='green', weight=155}]
System.out.println(filterApples(inventory, Ch01::isGreenApple));
//[Apple{color='green', weight=155}]
System.out.println(filterApples(inventory, Ch01::isHeavyApple));
}
}
728x90
'Language > 자바' 카테고리의 다른 글
자바 시그니처? (0) | 2023.01.29 |
---|---|
함수형 인터페이스 간략히 알아보자. (0) | 2023.01.29 |
자바 cpu 개수 구하기 (0) | 2023.01.28 |
StringTokenizer, split 차이점[공유링크] (0) | 2023.01.23 |
자바 Scanner vs BufferedReader (0) | 2023.01.22 |
댓글