Function
函数式接口抽象接口
apply
—将此函数应用于给定的参数默认实现接口
compose
—将compose
传入函数的返参作为当前函数的入参andThen
—将当前函数的返参作为andThen
的入参public class FunctionTest {
public static void main(String[] args) {
FunctionTest functionTest = new FunctionTest();
System.out.println(functionTest.compute(1, value -> 2 * value));
System.out.println(functionTest.compute(2, value -> 5 + value));
System.out.println(functionTest.compute(3, value -> value * value));
System.out.println(functionTest.convert(4, value -> value + " convert"));
//
Function<Integer, Integer> function = value -> 2 * value;
System.out.println(functionTest.compute(1, function));
}
public int compute(int a, Function<Integer, Integer> function){
int result = function.apply(a);
return result;
}
public String convert(int a, Function<Integer, String> function){
return function.apply(a);
}
}
高阶函数:
—接收一个函数作为参数,或者将一个函数作为返回值的函数
示例
public class FunctionTest2 {
public int composeTest(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
return function1.compose(function2).apply(a);
}
public static void main(String[] args) {
FunctionTest2 functionTest2 = new FunctionTest2();
System.out.println(functionTest2.composeTest(2, value -> value * 3, value -> value * value));
}
}
输出结果:
12
先执行function2
函数的执行作为function1
的参数
即2*2*3
=12
示例
public class FunctionTest2 {
public int andThenThes(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
return function1.andThen(function2).apply(a);
}
public static void main(String[] args) {
FunctionTest2 functionTest2 = new FunctionTest2();
System.out.println(functionTest2.andThenThes(2, value -> value * 3, value -> value * value));
}
}
输出结果:
36
先执行function1
函数的执行结果作为function2
的参数
即(2*3)*(2*3)
=36
BiFunction
双参数函数式接口抽象方法
apply
—将此函数应用于给定的两个参数andThen
—将当前函数两个参数执行后的返参作为andThen
的入参BiFunction
与Function
接口类似,不同点在于前者接收两个参数,后者只接收一个参数示例
public class FunctionTest2 {
public int bitest(int a, int b, BiFunction<Integer, Integer, Integer> biFunction){
return biFunction.apply(a, b);
}
public int bitAndThenTest(int a, int b, BiFunction<Integer, Integer, Integer> biFunction, Function<Integer, Integer> function){
return biFunction.andThen(function).apply(a, b);
}
public static void main(String[] args) {
FunctionTest2 functionTest2 = new FunctionTest2();
System.out.println(functionTest2.bitest(1, 2, (value1, value2) -> value1 + value2));
System.out.println(functionTest2.bitest(1, 2, (value1, value2) -> value1 - value2));
System.out.println(functionTest2.bitest(1, 2, (value1, value2) -> value1 * value2));
System.out.println(functionTest2.bitest(1, 2, (value1, value2) -> value1 / value2));
System.out.println(functionTest2.bitAndThenTest(2, 3, (value1, value2) -> value1 + value2, value -> value * value));
}
}
输出结果
3
-1
2
0
25
Predicate
断言函数式接口抽象接口
test()
—断言传入参数是否为真,抽象接口默认实现
and()
—两个断言进行与操作,默认实现negate()
—取当前断言的相关结果,及!test()
,默认实现or()
—两个断言进行或操作,默认实现public class PredicateTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 9, 10);
PredicateTest test = new PredicateTest();
test.conditionFilter(list, integer -> integer % 2 == 0);
System.out.println("-----");
test.conditionFilter(list, integer -> integer % 2 == 1);
System.out.println("-----");
test.conditionFilter(list, integer -> integer > 5);
System.out.println("-----");
test.conditionFilter(list, integer -> true);
System.out.println("-----");
test.conditionFilter(list, integer -> false);
}
public void conditionFilter(List<Integer> list, Predicate<Integer> predicate) {
list.forEach(integer -> {
if (predicate.test(integer)) {
System.out.println(integer);
}
});
}
}
执行结果
2
4
6
10
+++++
1
3
5
7
9
+++++
6
7
9
10
+++++
1
2
3
4
5
6
7
9
10
+++++
Supplier
函数式接口不接受参数,同时返回一个结果
public class SupplierTest1 {
public static void main(String[] args) {
Supplier<String> supplier = () -> "hello world";
System.out.println(supplier.get());
}
}
执行结果
hello world
public class SupplierTest2 {
public static void main(String[] args) {
Supplier<Student> supplier1 = () -> new Student();
System.out.println(supplier1.get().getName());
System.out.println("++++++++");
Supplier<String> supplier2 = Student::getStr;
System.out.println(supplier2.get());
}
}
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student() {
name = "zhangsan";
age = 20;
}
private static String str = "hello world";
public static String getStr() {
return str;
}
}
执行结果
zhangsan
++++++++
hello world
BinaryOperator
函数式接口继承自BiFunction
接口,不同点在于BinaryOperator
函数式接口的两个入参与返参类型一致
public class BinaryOperatorTest {
public static void main(String[] args) {
BinaryOperatorTest test = new BinaryOperatorTest();
System.out.println(test.compute(5, 3, (a, b) -> a + b));
System.out.println(test.compute(5, 3, (a, b) -> a - b));
System.out.println(test.getMin(5, 8, Integer::compareTo));
//比较长度
System.out.println(test.getShort("hello123", "world", (a, b) -> a.length() - b.length()));
//比较首字母
System.out.println(test.getShort("hello123", "world", (a, b) -> a.charAt(0) - b.charAt(0)));
}
public Integer compute(Integer a, Integer b, BinaryOperator<Integer> operator) {
return operator.apply(a, b);
}
public Integer getMin(Integer a, Integer b, Comparator<Integer> comparator){
return BinaryOperator.minBy(comparator).apply(a, b);
}
public String getShort(String a, String b, Comparator<String> comparator){
return BinaryOperator.minBy(comparator).apply(a, b);
}
}
执行结果
8
2
5
world
hello123