Map+函数式接口方法

记录一下Map+函数式接口方法,之前没有这样处理理,看了下公众号推送的资料和在网上看的,结合实际业务还是比较方便的

test

@Test
    void testMapFunction(){

        Map<String, Function<String,String>> grantTypeMap=new HashMap<>();
        grantTypeMap.put("hame",val->sendMsg(val));

        Function<String, String> hame = grantTypeMap.get("hame");
        String res = hame.apply("彪");
        System.out.println(res);
        String res1 = hame.apply("hame");
        System.out.println(res1);
        String res2 = hame.apply("");
        System.out.println(res2);
    }

    private String sendMsg(String s){
        switch (s){
            case "彪":
                return  "hamea";
            case "hame":
                return "彪";
            default:
                return "无效";
        }
    }
/**
相关资料
*/
  @Test
    void testFunction(){
        Function<Integer, Integer> func = e -> {return e + 5;};
        Function<Integer, Integer> func2 = e -> {return e * 5;};
//func2即after
        func.andThen(func2).apply(5); // 50
        System.out.println(func.andThen(func2).apply(5));



//      compose:  和andThen刚好相反:先使用操作before处理参数,再对返回值进行处理。
        Function<Integer, Integer> func3 = e -> {return e + 5;};
        Function<Integer, Integer> func4 = e -> {return e * 5;};
//func2即before
        func3.compose(func4).apply(5); // 30
        System.out.println( func3.compose(func4).apply(5));
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值