본문 바로가기

Java 길찾기/이것이 자바다

[Java] 표준 API의 함수적 인터페이스 - minBy(), maxBy() 정적 메소드

BinaryOperator<T> 함수적 인터페이스는 minBy()와 maxBy() 정적 메소드를 제공한다. 이 두 메소드는 매개값으로 제공되는 comparator를 이용해서 최대 T와 최소 T를 얻는다. 이 두 메소드는 매개값으로 제공되는 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<> {
    public int compare(T o1, T o2);
}

 

Comparator<T>를 타겟 타입으로 하는 람다식은 다음과 같이 작성할 수 있다.

(o1, o2) -> { ...; return int값; }

 

만약 o1과 o2가 int 타입이라면 다음과 같이 Integer.compare(int, int) 메소드를 이용할 수 있다. Integer.compare()는 첫 번째 매개값이 두 번째 매개값보다 작으면 음수, 같으면 0, 크면 양수를 리턴한다.

(o1, o2) -> Integer.compare(o1, o2);

 

다음 예제는 두 과일의 값을 비교해서 값이 낮거나 높은 과일을 얻어낸다.

// OperatorMinByMaxByExample.java -- minBy(), maxBy() 정적 메소드
import java.util.function.BinaryOperator;

public class OperatorMinByMaxByExample {
    public static void main(String[] args) {
        BinaryOperator<Fruit> binaryOperator;
        Fruit fruit;
        
        binaryOperator = BinaryOperator.minBy((f1, f2)->Integer.compare(f1.price, f2.price));
        fruit = binaryOperator.apply(new Fruit("딸기", 6000), new Fruit("수박", 10000));
        System.out.println(fruit.name);

        binaryOperator = BinaryOperator.maxBy((f1, f2)->Integer.compare(f1.price, f2.price));
        fruit = binaryOperator.apply(new Fruit("딸기", 6000), new Fruit("수박", 10000));
        System.out.println(fruit.name);
    }
}