Function优化大量的if else(案例一)

Function(Function<T, R> )可以看作转换型函数,有一个输入型参数T和返回型参数R。

package com;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class Function1Test {
    private static Map<String, Function<String,String>> map = new HashMap();
    public static void main(String[] args) {
        init();
        String type = "a";
//        if(type.equals("a")){
//            func01("A","B");
//        } else if(type.equals("b")){
//            func02("C","D");
//        }else if(type.equals("c")){
//            func03("E","F");
//        }else if(type.equals("d")){
//            func03("G","H");
//        }
        map.get(type).apply("A");
    }

    private static void init() {
        map.put("a",Function1Test::func01);
        map.put("b",Function1Test::func02);
        map.put("c",Function1Test::func03);
        map.put("d",Function1Test::func04);
    }

    public static String func01(String... str){
        System.out.println("================func01==============="+ Arrays.toString(str));
        return "func01";
    }

    public static String func02(String... str){
        System.out.println("================func02==============="+ Arrays.toString(str));
        return "func02";
    }

    public static String func03(String... str){
        System.out.println("================func03==============="+ Arrays.toString(str));
        return "func03";
    }

    public static String func04(String... str){
        System.out.println("================func04==============="+ Arrays.toString(str));
        return "func04";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泡^泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值