vlambda博客
学习文章列表

Java8 - 新功能之函数式接口BinaryOperator

BinaryOperator<T>函数式接口是BiFunction<T,T,T>的子接口,它接受2个都是T类型的参数,并且返回T类型的结果。

下面是这个接口的定义:

package java.util.function;

import java.util.Objects;
import java.util.Comparator;

/**
* Represents an operation upon two operands of the same type, producing a result
* of the same type as the operands. This is a specialization of
* {@link BiFunction} for the case where the operands and the result are all of
* the same type.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #apply(Object, Object)}.
*
* @param <T> the type of the operands and result of the operator
*
* @see BiFunction
* @see UnaryOperator
* @since 1.8
*/
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
/**
* Returns a {@link BinaryOperator} which returns the lesser of two elements
* according to the specified {@code Comparator}.
*
* @param <T> the type of the input arguments of the comparator
* @param comparator a {@code Comparator} for comparing the two values
* @return a {@code BinaryOperator} which returns the lesser of its operands,
* according to the supplied {@code Comparator}
* @throws NullPointerException if the argument is null
*/
public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
}

/**
* Returns a {@link BinaryOperator} which returns the greater of two elements
* according to the specified {@code Comparator}.
*
* @param <T> the type of the input arguments of the comparator
* @param comparator a {@code Comparator} for comparing the two values
* @return a {@code BinaryOperator} which returns the greater of its operands,
* according to the supplied {@code Comparator}
* @throws NullPointerException if the argument is null
*/
public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
}
}

下面对这个接口的方法如何使用进行举例说明:

例子1:apply抽象方法

private static void testBaseFunction() {
BinaryOperator<String> joinOperator = (t1,t2)-> t1 + "," + t2;
String str = joinOperator.apply("Hello", "World");
System.out.println("str:" + str);
}

输出:str:Hello,World

例子2:minBy静态方法

private static void testMinBy() {
//取2个整数的最小值
BinaryOperator<Integer> minOperator = BinaryOperator.minBy(Comparator.naturalOrder());
Integer minInt = minOperator.apply(203,500);
System.out.println("minInt:" + minInt);
}

输出:minInt:203

例子3:maxBy静态方法

private static void testMaxBy() {
//取2个字符串的最大值,小写字母排序要比大写字母大
BinaryOperator<String> maxOperator = BinaryOperator.maxBy(Comparator.naturalOrder());
String maxStr = maxOperator.apply("ABC","anc");
System.out.println("maxStr:" + maxStr);
}

输出:maxStr:anc

java的泛型不支持基础数据类型,因为jdk特别增加了DoubleBinaryOperator,IntBinaryOperator和LongBinaryOperator三个支持基础数据类型的函数式接口。它们的定义如下:

@FunctionalInterface
public interface DoubleBinaryOperator {
/**
* Applies this operator to the given operands.
*
* @param left the first operand
* @param right the second operand
* @return the operator result
*/
double applyAsDouble(double left, double right);
}
@FunctionalInterface
public interface IntBinaryOperator {

/**
* Applies this operator to the given operands.
*
* @param left the first operand
* @param right the second operand
* @return the operator result
*/
int applyAsInt(int left, int right);
}
@FunctionalInterface
public interface LongBinaryOperator {

/**
* Applies this operator to the given operands.
*
* @param left the first operand
* @param right the second operand
* @return the operator result
*/
long applyAsLong(long left, long right);
}

部分文字与图片来源于网络,如有版权请联系删除!