JAVA如何将函数作为方法参数传入

1、创建函数接口类

//函数接口类
public interface IFunction {
    Map<String, Object> queryYearData(Map<String, Object> map);
};

2、方法调用时,使用lambda表达式传入函数参数

public CommonResult<List<Map<String, Object>>> queryGeneralFollowUpYear(String loginType) {
    return queryYearData(loginType, (map) -> mapper.queryGeneralFollowUpYear(map));
}

3、具体方法实现

private CommonResult<List<Map<String, Object>>> queryYearData(String loginType, IFunction myFunction) {
    SysUser sysUser = SystemUtil.getLoginUser(loginType);
    if (null == sysUser){
        return  new CommonResult<>(CommonResultEmnu.INVALID_USER);
    }
    Map<String, Object> map = new HashMap<>();
    Long createSysId = sysUser.getId();
    map.put("createId", createSysId);
 
    Map<String, Object> recordYear = myFunction.queryYearData(map);
    return new CommonResult<>(ChartUtil.getLastTwelfthMonthData(recordYear));
}

扫描下方二维码,关注公众号:程序进阶之路,实时获取更多优质文章推送。


扫码关注

评论