文章目录
java.util.function包下有常见的函数式接口。还有一些根据基本类型封装好的接口,如IntFunction。
接口 | 描述 |
---|---|
Consumer | 接受一个输入参数,不返回结果 |
Function<T,R> | 接受一个输入参数,返回一个结果 |
Predicate | 接受一个输入参数,返回true或false |
Supplier | 无参数,返回一个结果 |
BiConsumer<T,U> | 接受两个输入参数,不返回结果 |
BiFunction<T,U,R> | 接受两个输入参数,返回一个结果 |
BiPredicate <T,U> | 接受两个输入参数,返回true或false |
1. Consumer
接受一个输入参数,不返回结果。
@Test
public void consumerTest() {
// 接受Integer
Consumer<Integer> consumer = x -> {
x += 1;
System.out.println(x);
};
consumer.accept(10);
// 接受Integer
IntConsumer intConsumer = x -> {
x += 1;
System.out.println(x);
};
intConsumer.accept(10);
}
1.1 BiConsumer
接受两个输入参数,不返回结果.
@Test
public void biConsumerTest() {
BiConsumer<Integer, Double> biConsumer = (a, b) -> {
System.out.println("Integer " + a + " Double " + b);
};
biConsumer.accept(1, 2.0);
}
2. Function
接受一个输入参数,返回一个结果。
@Test
public void functionTest1() {
// 接受Integer,返回Integer
Function<Integer, Integer> function = x -> x + x;
System.out.println(function.apply(1));
// 接受Integer,并返回Double
IntFunction<Double> intFunction = x -> x + 0.1;
System.out.println(intFunction.apply(1));
}
函数式接口的注解@FunctionalInterface
是可选的。
作用:接口中如果有多个抽象方法则会产生编译期错误。即使用了该注解,就只能有一个抽象方法。
@FunctionalInterface
interface FunctionTest2 {
int run(int a);
}
@Test
public void functionTest2() {
FunctionTest2 function = x -> x + x;
System.out.println(function.run(1));
}
函数组合,保证输入和输出类型相对应。
@Test
public void functionTest3() {
Function<Integer, Integer> function1 = x -> {
System.out.println("1: " + x);
return x + x;
};
Function<Integer, Integer> function2 = x -> {
System.out.println("2: " + x);
return x + x;
};
Function<Integer, Integer> function3 = x -> {
System.out.println("3: " + x);
return x + x;
};
// 先执行function3,再function1,最后function2
Function<Integer, Integer> function4 = function1.compose(function3).andThen(function2);
System.out.println(function4.apply(1));
/**
* 执行结果
* 3: 1
* 1: 2
* 2: 4
* 8
*/
}
@Test
public void functionTest4() {
Function<Integer, Float> function1 = x -> {
System.out.println("1: " + x);
return x + 0.1f;
};
Function<Float, Double> function2 = x -> {
System.out.println("2: " + x);
return x + 0.1;
};
Function<Integer, Double> function3 = function1.andThen(function2);
System.out.println(function3.apply(1));
/**
* 执行结果
* 1: 1
* 2: 1.1
* 1.200000023841858
*/
}
2.1 UnaryOperator
继承自Function,输入和输出返回类型相同。
@Test
public void unaryOperatorTest() {
// 接受Integer,返回Integer
UnaryOperator<Integer> unaryOperator = x -> {
System.out.println(x);
return x + x;
};
System.out.println(unaryOperator.apply(1));
// 接受Integer,返回Integer
IntUnaryOperator intUnaryOperator = x -> {
System.out.println(x);
return x + x;
};
System.out.println(intUnaryOperator.applyAsInt(1));
}
2.2 BiFunction
接受两个输入参数,返回一个结果。
@Test
public void biFunctionTest() {
// 接受Integer,Double,返回Double
BiFunction<Integer, Double, Double> biFunction = (a, b) -> {
System.out.println("Integer " + a + " Double " + b);
return a + b;
};
System.out.println(biFunction.apply(1,2.0));
// 接受Integer,Double,返回Double
ToDoubleBiFunction<Integer, Double> toDoubleBiFunction = (a, b) -> {
System.out.println("Integer " + a + " Double " + b);
return a + b;
};
System.out.println(toDoubleBiFunction.applyAsDouble(1,2.0));
}
2.2.1 BinaryOperator
继承自BiFunction,两个输入类型和返回类型相同。
@Test
public void binaryOperatorTest() {
BinaryOperator<String> binaryOperator1 = (a, b) -> a + b;
System.out.println(binaryOperator1.apply("abc", " c"));
IntBinaryOperator intBinaryOperator = (a, b) -> a + b;
System.out.println(intBinaryOperator.applyAsInt(1,2));
}
3. Predicate
接受一个输入参数,返回true或false。
@Test
public void predicateTest1() {
Predicate<String> p1 = s -> s.contains("a");
System.out.println(p1.test("abc"));
System.out.println(p1.test("bc"));
}
函数组合
@Test
public void predicateTest2() {
Predicate<String> p1 = s -> s.contains("a");
Predicate<String> p2 = s -> s.length() < 3;
Predicate<String> p3 = s -> s.contains("b");
// 不包含a并且长度小于3 或者 包含b 或者等于fff
Predicate<String> p4 = p1.negate().and(p2).or(p3).or(Predicate.isEqual("fff"));
Stream.of("aa","bbbbb","cc","ddddd","fff")
.filter(p4)
.forEach(System.out::println);
/**
* 执行结果
* bbbbb
* cc
* fff
*/
}
3.1 BiPredicate
接受两个输入参数,返回true或false。
@Test
public void biPredicateTest() {
BiPredicate<Integer, Integer> biPredicate = (a,b) -> a > b;
System.out.println(biPredicate.test(1,2));
}
4. Supplier
无参数,返回一个结果。
@Test
public void supplierTest() {
Supplier<Double> supplier = () -> {
System.out.println("supplier");
return Math.random();
};
System.out.println(supplier.get());
}