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, U
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值