[Sleeping JDK] Detailed explanation of Java functional programming interface UnaryOperator, BinaryOperator

The two brothers Big U (Unary: one yuan) and Big B (Binary: binary) introduced today are actually not brand new things, but two special cases. Let’s first look at Kangkang’s source code.

UnaryOperator source code :

@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {
    
    
    static <T> UnaryOperator<T> identity() {
    
    
        return t -> t;
    }
}

Source code of BinaryOperator:

@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
    
    
    
    public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
    
    
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
    }

    public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
    
    
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
    }
}

It can be seen from the source code that UnaryOperator inherits from Function<T, T>, and the parameters and return values ​​of Function are of the same type T. Therefore, when the parameters and return values ​​of Function are of the same type, we can use UnaryOperator instead of Function. In the same way, the same is true for BinaryOperator, which inherits from BiFunction<T, T, T>. When the return value and the types of the two parameters are the same, BinaryOperator can be used instead of BiFunction. This is also done to simplify development. JDK only provides univariate and binary implementations. Of course, if you have needs, you can also implement TernaryOperator (ternary) yourself.

Function, as the function programming interface that comes with JDK, has many variations, and its status is evident, so friends must read more and think more. JDK gives us a lot of cabbage, but I hope you can plant it in your heart. Their seeds, learn to integrate them, learn and apply them flexibly.

There are also some variants of Operator, as follows:

Interface name parameter Return type describe
IntUnaryOperator int int Both parameters and return values ​​are of type int.
DoubleUnaryOperator double double Parameters and return values ​​are of double type
LongUnaryOperator long long Parameters and return values ​​are all of type long
IntBinaryOperator (int, int) int Both parameters and return value are of type int.
DoubleBinaryOperator (double, double) double Both parameters and return value are of type double.
LongBinaryOperator (long, long) long Both parameters and return value are of type long

Okay, now all the important functional programming interfaces of JDK have been introduced.

Introduction to other functional programming interfaces:

[Sleeping JDK] Detailed explanation of Java functional programming interface Predicate

[Sleeping JDK] Detailed explanation of Java functional programming interface Consumer, Function

[Sleeping JDK] Detailed explanation of Java functional programming interface Supplier

end
Java Development Paradise

Guess you like

Origin blog.csdn.net/u012534326/article/details/107031082
Recommended