Java 8中的Lambda表达式是一种匿名函数,它允许你将函数作为方法参数进行传递,或者把代码更简洁地定义在你的应用程序里。另外Java的函数式编程就是Lambda表达式,java的函数式接口的有一个明显特征:有且仅有一个抽象方法的接口。下面是一些常见的Java内置的函数式接口梳理,小伙伴们可以一起看看。
一、Function函数接口
Function函数接口:接收一个参数,返回一个结果的函数
1.Function简单用法
@Test
public void test1_String(){
//输入一个String类型的参数,输出一个String类型的结果
// 第一个参数--入参:String
// 第二个参数--出参:String
Function<String ,String> functionStr = str->str+" hello world!";
System.out.println("---------");
String funStr = functionStr.apply("java function");
System.out.println(funStr);
//输出:java function hello world!
}
@Test
public void test2_Integer(){
//输入一个Integer类型的参数,输出一个String类型的结果
Function<Integer,String> functionStr = str-> String.valueOf(str + 123);
System.out.println("---------");
System.out.println(functionStr.apply(1123));
//输出:1246
}
@Test
public void test3_StrHandle(){
//输入一个String类型的参数,输出一个String类型的结果
//输入一个字符串,将其转为大写并从第一位开始截取到最后一位字符返回
Function<String,String> functionStr = str -> {
String upcaseStr = str.toUpperCase();
return upcaseStr.substring(0,str.length()-1);
};
System.out.println("---------");
//执行输入一个字符串
System.out.println(functionStr.apply("hello world java function test!"));
//输出:HELLO WORLD JAVA FUNCTION TEST
}
2.Function函数接口中的compose方法
compose方法:组合函数执行
@Test
public void test4_compose(){
//定义functionStr函数,将入参转为大写后返回
Function<String,String> functionStr = str -> str.toUpperCase();
//定义functionSubstr函数,将入参从第一位截取到倒数第二位
Function<String,String> functionSubstr = substr -> substr.substring(0,substr.length()-1);
Function<String,String> functionStr3 = str -> str + " 你好!";
//定义functionCompose组合函数,将两个函数合并为一个新的函数执行
//functionCompose函数逻辑:
//1、将functionStr函数的返回值给到functionSubstr
//2、functionSubstr函数接收到functionStr函数的返回值后,再执行自己本身函数的逻辑
//执行顺序:从右向左执行
Function<String,String> functionCompose = functionStr.compose(functionSubstr).compose(functionStr3);
System.out.println(functionCompose.apply("hello world java function test!"));
//输出:HELLO WORLD JAVA FUNCTION TEST! 你好
}
3.Function函数接口中的andThen方法
andThen方法:组合函数执行
@Test
public void test5_andThen(){
// functionStr--接收参数并将其转为大写并返回
// functionStr--接收一个参数并将其返回的结果作为参数去执行function2函数
// function2--接收参数并拼接指定字符串返回
//function2--接收由functionStr函数返回的值并将其作为输入参数拼接自己函数内部指定字符串并返回
Function<String,String> functionStr = str -> str.toUpperCase();
Function<String,String> functionStr2 = str -> str + " addThen function world";
Function<String,String> functionStr3 = str -> str + " 你好!";
//执行顺序:从左向右执行
Function<String,String> addThenFun = functionStr.andThen(functionStr2).andThen(functionStr3);
System.out.println(addThenFun.apply("hello function!"));
//输出:HELLO FUNCTION! addThen function world 你好!
}
compose与andThen总结:
Function函数接口中compose与andThen方法均可实现函数组合的作用,但两者的执行顺序不同。
compose函数:优先执行调用者的apply方法,说人话就是:函数从右向左执行。执行完右边函数后,将其返回的结果作为参数给到左边(上一个)的函数执行。
andThen函数:优先执行函数内的apply方法,说人话就是:函数从左向右执行。执行完左边函数后,将其返回的结果作为参数给到右边(下一个)的函数执行。
二、BiFunction函数接口
BiFunction函数接口:接收两个参数,返回一个结果
1.BiFunction函数接口简单使用
@Test
public void test1(){
//输入两个参数,返回一个结果
BiFunction<Integer,Integer,String> biFunction = (x, y) -> String.valueOf(x * y);
System.out.println(biFunction.apply(3,5));
//输出:15
}
2.BiFunction函数接口中的andThen方法
andThen方法:组合函数与Function函数接口中的andThen方法作用于执行顺序一致。
@Test
public void test2_andThen(){
//定义BiFunction函数,接收两个String类型的入参,将这两个参数按指定规则拼接并返回
BiFunction<String,String,String> biFunction = (x,y) -> x + " join " + y;
//定义Function函数,接收biFunction函数返回的参数作为入参将其转为大写并返回
Function<String,String> function = str -> str.toUpperCase();
//使用andThen方法,组合biFunction函数与function函数
//执行顺序:由左往右执行
BiFunction<String,String,String> andThenBIFunction = biFunction.andThen(function);
System.out.println(andThenBIFunction.apply("hello","world"));
//输出:HELLO JOIN WORLD
}
三、Predicate函数接口
Predicate函数接口,通常用于条件判断。接收一个参数,返回布尔值(true/false)
1.Predicate函数接口的简单使用
test方法:调用执行函数,根据条件判断是否符合指定条件。
@Test
public void test1(){
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
Predicate< List<Integer> > predicate = pre -> list.contains(7);
//test()方法,判断是否满足指定条件,返回true/false
System.out.println("pridecate函数TEST方法返回结果:"+ predicate.test(list));
//断言:判断返回的结果必须是true,否则抛出异常
// Assert.isTrue(predicate.test(list),"test error");
//输出:pridecate函数TEST方法返回结果:false
}
四、Supplier函数接口
Supplier函数接口:不接收输入参数,返回指定类型的结果
1.Supplier函数接口简单使用
@Test
public void test(){
Supplier<Integer> supplierRandom = () -> new Random().nextInt();
System.out.println("supplier调用gey()函数:"+supplierRandom.get());
//输出:supplier调用gey()函数:-1232030092
}
@Test
public void test_Map(){
//无输入参数
//返回任意类型
Supplier<Map> supplierMap = () -> {
Map map = new HashMap<>();
map.put("name","张三");
map.put("age","18");
map.put("sex","男");
map.put("hobby","打篮球,看书");
return map;
};
Map callbackMap = supplierMap.get();
System.out.println(callbackMap);
//输出:{sex=男, name=张三, age=18, hobby=打篮球,看书}
}
五、Consumer函数接口
consumer函数接口:接收参数无返回值
1.Consumer函数接口简单使用
@Test
public void test1(){
List list = new ArrayList();
Consumer<Integer> consumer = con -> {
list.add(con);
};
//Consumer:接收参数无返回值
consumer.accept(1);
System.out.println(list);
//输出:[1]
}
@Test
public void test2(){
List list = new ArrayList();
Map map = new HashMap<>();
map.put("name","zhangsan");
list.add(map);
Map map2 = new HashMap<>();
map2.put("name","lisi");
list.add(map2);
List<Map> callbackList = new ArrayList();
Consumer<List<Map>> consumer = con -> {
Map fillMap = new HashMap();
String name = con.stream()
.map(mapData->mapData.get("name").toString())
.collect(Collectors.joining("-"));
fillMap.put("name",name.toUpperCase());
callbackList.add(fillMap);
};
//Consumer:接收参数无返回值
consumer.accept(list);
System.out.println(callbackList);
//输出:[{name=ZHANGSAN-LISI}]
}