记录一下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));
}