Formoat's Open Blog
함수적 인터페이스 - Operator 본문
1. Operator
1) 매개값과 반환값이 모두 있는 함수적 인터페이스
2) 매개값과 반환값이 모두 있는 applyXXX() 메소드를 가지고 있다.
3) 이들 메소드는 매개값을 이용해 연산후 동일한 타입으로 결과를 반환한다.
4) Function과 다른점은 매개값과 반환값의 타입이 같다는 것이다.
Interface Name | Abstract Method | Remarks |
BinaryOperator<T> | T apply(T t1, T t2) | 두 개의 T 연산 후 반환 |
UnaryOperator<T> | T apply(T t) | 하나의 T 연산 후 반환 |
DoubleBanaryOperator | double applyAsDouble(double value1, double value2) | 두 개의 double 연산 후 반환 |
DoubleUnaryOperator | double applyAsDouble(double value) | 하나의 double 연산 후 반환 |
IntBinaryOperator | int applyAsInt(int value1, int value2) | 두 개의 int 연산 후 반환 |
IntUnaryOperator | int applyAsInt(int value) | 하나의 int 연산 후 반환 |
LongBinaryOperator | long applyAsLong(long value1, long value2) | 두 개의 long 연산 후 반환 |
LongUnaryOperator | long applyAsLong(long value) | 하나의 long 연산 후 반환 |
<표> Operator 함수적 인터페이스의 종류
OperatorExample.java
public class OperatorExample {
private static int[] arr = { 80,100,90,110,70 };
public static int maxOrMin(IntBinaryOperator operator) {
int result = arr[0];
for (int a : arr) {
result = operator.applyAsInt(result, a);
}
return result;
}
public static void main(String[] args) {
int max = maxOrMin((x, y) -> x > y ? x : y);
int min = maxOrMin((x, y) -> x < y ? x : y);
System.out.println("max:"+max);
System.out.println("min:"+min);
}
}
// 실행결과 //
----------------------------------------------------
max:110
min:70
2. 정적 메소드
1) minBy(), maxBy() 정적 메소드
(1) BinaryOperator<T> 함수적 인터페이스가 가지고 있는 정적 메소드
(2) 매개값으로 Comparator 함수적 인터페이스를 사용해 두 객체를 비교 한다.
(3) Comparator는 두 객체를 비교해 작다면 음수, 같다면 0, 크다면 양수를 반환한다.
Return Type | Abstract Method |
BinaryOperator<T> | minBy(Comparator<? super T> comparator) |
BinaryOperator<T> | maxBy(Comparator<? super T> comparator) |
<표> minBy(), maxBy() 정적 메소드의 반환타입과 매개변수
# Comparator<T> 함수적 인터페이스의 구성
@FunctionalInterface
public interface Comparator<T> {
public int compare(T o1, T o2);
}
// Comparator<T>를 타켓타입으로 하는 람다식은 아래와 같이 표현할 수 있다.
(o1, o2) -> { o1 < o2 ? -1 : (o1 == o2) ? 0 : 1 };
(o1, o2) -> Integer.compare(o1, o2);
ComparatorExample.java
public class ComparatorExample {
public static void main(String[] args) {
BinaryOperator<Student> binaryOpt;
Student student;
binaryOpt = BinaryOperator.maxBy((s1, s2) -> Integer.compare(s1.getScore(), s2.getScore()));
student = binaryOpt.apply(new Student("홍길동", "남자", 100), new Student("김사랑", "여자", 99));
System.out.println("maxBy: " + student.getName());
binaryOpt = BinaryOperator.minBy((s1, s2) -> Integer.compare(s1.getScore(), s2.getScore()));
student = binaryOpt.apply(new Student("홍길동", "남자", 100), new Student("김사랑", "여자", 99));
System.out.println("minBy: " + student.getName());
}
}
// 실행결과 //
----------------------------------------------------
maxBy: 홍길동
minBy: 김사랑
'Java > Java Basic' 카테고리의 다른 글
컬렉션 프레임워크 (Collection Framework) (0) | 2019.09.03 |
---|---|
함수적 인터페이스 - Predicate (0) | 2019.09.03 |
함수적 인터페이스 - Function (0) | 2019.09.03 |
함수적 인터페이스 - Supplier (0) | 2019.09.03 |
함수적 인터페이스 - Consumer (0) | 2019.09.03 |
Comments