class Solution {
public int solution(String ineq, String eq, int n, int m) {
Map<String, BiFunction<Integer, Integer, Boolean>> functions = Map.of(
">=", (a, b) -> a >= b,
"<=", (a, b) -> a <= b,
">!", (a, b) -> a > b,
"<!", (a, b) -> a < b
);
return functions.get(ineq + eq).apply(n, m) ? 1 : 0;
}
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t, U u) -> after.apply(apply(t, u));
}
}
제네릭 타입인 두 개의 인수, 제네릭 타입을 반환
BiFunction 인터페이스는 세 개의 제네릭 타입을 사용
T: 첫 번째 매개변수의 타입
U: 두 번째 매개변수의 타입
R: 반환 타입
인터페이스 내부에는 추상 메서드 apply()와 디폴트 메서드인 andThen() 메서드가 존재
람다 표현식을 사용하면 BiFunction 인터페이스의 추상 메서드를 구현하는 클래스를 정의할 필요가 없으며, BiFunction 타입의 객체에 할당된 람다 표현식은 apply() 메서드를 구현하기 위해 사용.
'코테 공부 > java' 카테고리의 다른 글
Collections.max(컬렉션 객체명) .min(컬렉션 객체명) (0) | 2023.05.03 |
---|---|
Arrays.copyOf, Arrays.copyOfRange, System.arraycopy() -배열 복사 (0) | 2023.05.03 |
equals(), == 차이 (0) | 2023.05.01 |
Integer .valueOf(str) .parseInt(str) 차이 (0) | 2023.05.01 |
str.substring() (0) | 2023.05.01 |