람다식 ④
and(), or(), negate() 디폴트 메소드
Predicate 함수적 인터페이스가 가지고 있는 and(), or(), negate() 디폴트 메소드에 대해서 알아보자
- and() : &&과 대응 - 두 Predicate가 모두 true를 리턴하면 최종적으로 true를 리턴
predicateAB = predicateA.and(predicateB);
- or() : ||과 대응 - 두 Predicate 중 하나만 true를 리턴하면 최종적으로 true를 리턴
predicateAB = predicateA.or(predicateB);
- negate() : !과 대응 - Predicate의 결과가 true 이면 false, false 이면 true를 리턴
predicateAB = predicateA.negate();
종류
|
함수적 인터페이스
|
and()
|
or()
|
negate()
|
Predicate
|
Predicate<T>
|
O
|
O
|
O
|
BiPredicate<T,U>
|
O
|
O
|
O
|
|
DoublePredicate
|
O
|
O
|
O
|
|
IntPredicate
|
O
|
O
|
O
|
|
LongPredicate
|
O
|
O
|
O
|
Example
public class PredicateAndOrNegateExample{
public static void main(String[] args){
//2의 배수를 검사
IntPredicate predicateA = a -> a%2 == 0;
//3의 배수를 검사
IntPredicate predicateB = a -> a%3 == 0;
IntPredicate predicateAB;
boolean result;
//and()
predicateAB = predicateA.and(predicateB);
result = predicateAB.test(9);
System.out.println("9는 2와 3의 배수 입니까? " + result); //출력: 9는 2와 3의 배수 입니까? false
//or()
predicateAB = predicateA.or(predicateB);
result = predicateAB.test(9);
System.out.println("9는 2와 3의 배수 입니까? " + result); //출력: 9는 2와 3의 배수 입니까? true
//negate()
predicateAB = predicateA.negate();
result = predicateAB.test(9);
System.out.println("9는 홀수 입니까 ? " + result); //출력: 9는 홀수 입니까? true
}
}
isEqual() 정적 메소드
Predicate 함수적 인터페이스가 가지고 있는 isEqual() 정적 메소드에 대해서 알아보자
Predicate<Object> predicate = Predicate.isEqual(targetObject);
boolean result = predicate.test(sourceObject);
Predicate에 정적 메소드인 isEqual()를 호출할 때 비교할 targetObject을 매개 값으로 제공해 주고 test()를 호출할 때 targetObject와 비교할 sourceObject 객체를 매 개값으로 주게 되면 두 객체를 비교하게 되는데 isEqual()로 비교할 때 Objects.equals(sourceObject, targetObject)를 이용해서 나온 결과를 test()에 결과로 리턴한다.
[ Objects.equals(sourceObject, targetObject)는 다음과 같은 리턴 값을 제공 ]
↓
sourceObject
|
targetObject
|
리턴 값
|
null
|
null
|
true
|
not null
|
null
|
false
|
null
|
not null
|
false
|
not null
|
not null
|
sourceObject.equals(targetObject)의 리턴 값
|
Example
public class PredicateIsEqualExample{
public static void main(String[] args){
Predicate<String> predicate;
predicate = Predicate.isEqual(null);
System.out.println("null, null : " predicate.test(null)); //출력: null, null : true
predicate = Predicate.isEqual("java8");
System.out.println("null, java8 : " predicate.test(null)); //출력: null, java8 : false
predicate = Predicate.isEqual(null);
System.out.println("java8, null : " predicate.test("java8")); //출력: java8, null : false
predicate = Predicate.isEqual("java8");
System.out.println("java8, java8 : " predicate.test("java8")); //출력: java8, java8 : true
}
}
minBy(), maxBy() 정적 메소드
BinaryOperator<T> 함수적 인터페이스가 가지고 있는 minBy(), maxBy() 정적 메소드에 대해서 알아보자
minBy()와 maxBy()는 Comparator를 이용해서 최대 T와 최소 T를 얻는 BinaryOperator<T>를 리턴한다.
리턴 타입
|
정적 메소드
|
|
BinaryOperator<T>
|
minBy(Comparator<? super T> comparator)
|
|
BinaryOperator<T>
|
maxBy(Comparator<? super T> comparator)
|
Comparator<T>는 아래 코드와 같이 선언된 함수적 인터페이스이다. o1과 o2를 비교해서 o1이 작으면 음수를 o1과 o2가 동일하면 0 o1이 크면 양수를 리턴해야 하는 compare() 메소드가 선언되어 있다.
@FunctionalInterface
public interface Comparator<T>{
public int compare(T o1, T o2);
}
Comparator<T>를 타겟 타입으로 하는 람다식을 작성할 때는 아래와 같이 작성할 수 있다. o1과 o2 매개변수를 작성하고 실행부에는 o1과 o2를 비교하는 코드를 직접 작성해 주면 된다.
(o1, o2) -> { ...; return iut 값; }
만약 o1과 o2가 int 타입이라면 Integer.compare(int, int) 메소드를 이용할 수 있다. 직업 비교하는 실행 부를 작성 안 해도 된다.
(o1, o2) -> Integer.compare(o1, o2);
Example
public class Fruit{
private String name;
private Integer price;
public Fruit(String name, Integer price){
this.name = name;
this.prive = price
}
public String getName(){
return name;
}
public Integer getPrice(){
return price;
}
}
public class OperatorMinByMaxByExample{
public static void main(String[] args){
BinaryOperator<Fruit> binaryOperator;
Fruit fruit;
//minBy
binaryOperator = BinaryOperator.minBy( (f1, f2) -> Integer.compare(f1.getPrice(), f2.getPrice()) );
fruit = binaryOperator.apply( new Fruit("딸기", 6000), new Fruit("수박", 10000) );
System.out.println( fruit.getName() ); //출력: 딸기
//maxBy
binaryOperator = BinaryOperator.maxBy( (f1, f2) -> Integer.compare(f1.getPrice(), f2.getPrice()) );
fruit = binaryOperator.apply( new Fruit("딸기", 6000), new Fruit("수박", 10000) );
System.out.println( fruit.getName() ); //출력: 수박
}
}
minBy는 매 개값으로 제공된 비교자 comparator가 음수에 값을 리턴한다면 f1을 리턴하는 BinaryOperator를 만든다. 반대로 comparator가 양수에 값을 리턴한다면 f2를 리턴하는 BinaryOperator를 만든다.
maxBy는 매 개값으로 제공된 비교자 comparator가 양수에 값을 리턴한다면 f1을 리턴하는 BinaryOperator를 만든다. 반대로 comparator가 음수에 값을 리턴한다면 f2를 리턴하는 BinaryOperator를 만든다.