Lambda中Function函数接口(三)——Operator系列接口

函数式接口汇总链接

Operator接口表示传入的参数类型和返回值类型一致,分为一元操作和二元操作两种,BinaryOperator、UnaryOperator 。

目录

UnaryOperator

IntUnaryOperator

LongUnaryOperator

DoubleUnaryOperator

BinaryOperator

IntBinaryOperator

LongBinaryOperator

DoubleBinaryOperator


  1. UnaryOperator

    UnaryOperator是一元操作接口,也就是只能接收一个参数。UnaryOperator继承了Function接口。所以在操作上可以使用Function中的方法。
    @FunctionalInterface
    public interface UnaryOperator<T> extends Function<T, T> {
    
        /**
         * Returns a unary operator that always returns its input argument.
         *
         * @param <T> the type of the input and output of the operator
         * @return a unary operator that always returns its input argument
         */
        static <T> UnaryOperator<T> identity() {
            return t -> t;
        }
    }

    代码示例

    public class TestUnaryOperator {
    
        public static void main(String[] args) {
            test(100, val -> val * 5);
            testAndThen1("test", str -> str.replaceAll("t", " operator "), a -> a.toUpperCase());
            testAndThen2("test UnaryOperator of andThen", obj -> obj.toUpperCase(), str -> str.contains("UnaryOperator"));
            testCompose(new User("张三", 33, "北京市朝阳区"), User::getAddress, str -> {
                if (str.contains("北京市")) {
                    return str;
                } else if (str.contains("上海市")) {
                    return "外地";
                } else {
                    return "未知";
                }
            });
        }
    
    // 测试apply方法
        public static void test(int val, UnaryOperator<Integer> operator) {
            Integer apply = operator.apply(val);
            System.out.println(apply);
        }
    
    // 测试andThen方法,注意传参
        public static void testAndThen1(String val, UnaryOperator<String> operator, UnaryOperator<String> after) {
            Function<String, String> andThen = operator.andThen(after);
            String apply = andThen.apply(val);
            System.out.println(apply);
        }
    
    // 测试andThen方法,注意传参
        public static void testAndThen2(String val, UnaryOperator<String> operator, Function<String, Boolean> after) {
            Function<String, Boolean> andThen = operator.andThen(after);
            Boolean apply = andThen.apply(val);
            System.out.println(apply);
        }
    
    // 测试compose方法,参数为Function<T,U>
        public static void testCompose(User user, Function<User, String> before, UnaryOperator<String> operator) {
            Function<User, String> compose = operator.compose(before);
            String apply = compose.apply(user);
            System.out.println(apply);
        }
    }

     

  2. IntUnaryOperator

    这个函数式接口操作的参数只能是int类型,而且只能是一个参数,并返回int类型的结果值。首先看一下源码
    @FunctionalInterface
    public interface IntUnaryOperator {
    
        /**
         * 将此方法应用于给定的参数
         */
        int applyAsInt(int operand);
    
        /**
         * 先用before对象执行给定的参数,然后将其结果对象执行applyAsInt方法。
         */
        default IntUnaryOperator compose(IntUnaryOperator before) {
            Objects.requireNonNull(before);
            return (int v) -> applyAsInt(before.applyAsInt(v));
        }
    
        /**
         * 先用当前的IntUnaryOperator对象执行传入的参数,然后再用after对象执行其结果。
         */
        default IntUnaryOperator andThen(IntUnaryOperator after) {
            Objects.requireNonNull(after);
            return (int t) -> after.applyAsInt(applyAsInt(t));
        }
    
        /**
         * 返回一元运算符,该运算符始终返回其输入参数。
         */
        static IntUnaryOperator identity() {
            return t -> t;
        }
    }

    使用方法如下:

    public class TestIntUnaryOperator {
        public static void main(String[] args) {
    
            // 将参数乘以2再减去1。最后结果:199
            testApply(100, val -> val * 2 - 1);
    
            // 先将参数乘以2,然后其结果再加上2  。最后结果:220
            testAndThen(100, val -> val * 2, a -> a + 2);
    
            // 先将参数减去10,然后将其结果乘以2。最后结果:180
            testCompose(100, a -> a - 10, val -> val * 2);
    
        }
    
        public static void testApply(int val, IntUnaryOperator operator) {
            int apply = operator.applyAsInt(val);
            System.out.println(apply);
        }
    
        public static void testAndThen(int val, IntUnaryOperator operator, IntUnaryOperator after) {
            IntUnaryOperator andThen = operator.andThen(after);
            int apply = andThen.applyAsInt(val);
            System.out.println(apply);
        }
    
        public static void testCompose(int val, IntUnaryOperator before, IntUnaryOperator operator) {
            IntUnaryOperator compose = operator.compose(before);
            int apply = compose.applyAsInt(val);
            System.out.println(apply);
        }
    }

     

  3. LongUnaryOperator

    操作的参数类型是Long类型的,返回值也是Long类型,参考IntUnaryOperator接口的使用。
  4. DoubleUnaryOperator

    操作的参数类型是Double类型的,返回值也是Double类型,参考IntUnaryOperator接口的使用。
  5. BinaryOperator

    BinaryOperator是二元函数接口,可以操作两个参数进行计算。继承了BiFunction接口,源码如下:
    @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;
        }
    }

    BinaryOperator对象还可以使用

    R apply(T t, U u)和default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after)两个方法。
    public class TestBinaryOperator {
        public static void main(String[] args) {
            // 将两个参数相加
            test(100, 200, (a, b) -> a + b);
            // 先让两个参数相加,然后再乘以2
            testAndThen(100, 200, (a, b) -> a + b, val -> val * 2);
            // 找出两个参数中小的那个
            testMinBy(100, 200, Integer::compareTo);
            // 找出两个参数中大的值
            testMayBy(100, 200, (x, y) -> (x < y) ? -1 : ((x == y) ? 0 : 1));
    
        }
    
        public static void test(int val1, int val2, BinaryOperator<Integer> operator) {
            Integer apply = operator.apply(val1, val2);
            System.out.println(apply);
        }
    
        public static void testAndThen(int val1, int val2, BinaryOperator<Integer> operator, Function<Integer, Integer> after) {
            BiFunction<Integer, Integer, Integer> andThen = operator.andThen(after);
            Integer apply = andThen.apply(val1, val2);
            System.out.println(apply);
        }
    
        public static void testMinBy(int val1, int val2, Comparator<Integer> comparator) {
            BinaryOperator<Integer> minBy = BinaryOperator.minBy(comparator);
            Object apply = minBy.apply(val1, val2);
            System.out.println(apply);
        }
    
        public static void testMayBy(int val1, int val2, Comparator<Integer> comparato) {
            BinaryOperator<Integer> maxBy = BinaryOperator.maxBy(comparato);
            Integer apply = maxBy.apply(val1, val2);
            System.out.println(apply);
        }
    }

     

  6. IntBinaryOperator

    对两个int类型的参数进行操作,只有一个方法,如下:
    @FunctionalInterface
    public interface IntBinaryOperator {
    
        int applyAsInt(int left, int right);
    }

    示例代码如下:

    public class TestIntBinaryOperator {
    
        public static void main(String[] args) {
            test(100, 200, (x, y) -> x * y);
        }
    
        public static void test(int val1, int val2, IntBinaryOperator operator) {
            int apply = operator.applyAsInt(val1, val2);
            System.out.println(apply);
        }
    }

     

  7. LongBinaryOperator

    操作的参数为Long类型,用法参考IntBinaryOperator。
  8. DoubleBinaryOperator

  9. 操作的参数为Double类型,用法参考IntBinaryOperator。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值