基于Java8函数编程案例
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import org.junit.Test;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class JDK8Test {
/*****************************
*
* Predicate Predicate<T> 接收T对象并返回boolean<br>
* Consumer Consumer<T> 接收T对象,不返回值<br>
* Function Function<T, R> 接收T对象,返回R对象<br>
*
* BiPredicate BiPredicate<T, U> 接收T对象和U对象,返回boolean<br>
* BiConsumer BiConsumer<T, U> 接收T对象和U对象,不返回值<br>
* BiFunction BiFunction<T, U, R> 接收T对象和U对象,返回R对象<br>
*
* BinaryOperator BinaryOperator<T> 接收两个T对象,返回T对象<br>
* Supplier Supplier<T> 提供T对象(例如工厂),不接收值<br>
* UnaryOperator UnaryOperator<T> 接收T对象,返回T对象<br>
*
*****************************/
class TestBean {
private int num;
public TestBean() {
this.num = 0;
}
public TestBean(int num) {
this.num = num;
}
public boolean check() {
return this.num > 0;
}
public void showMsg(String msg) {
System.out.println("TestBean::showMsg => " + msg);
}
public String getMsg(String msg) {
return String.format("num:%s msg:%s", num, msg);
}
}
/*************************************
*
* Predicate
*
*************************************/
@Test
public void test_Predicate() {
// 接收一个 布尔值 类型的返回结果
// 不能指定输入参数
Predicate<String> predicate = x -> null == x;
boolean test = predicate.and(x -> null == x).test("1");
boolean test1 = predicate.and(x -> null == x).test(null);
log.info("test:{} test1:{}", test, test1);
boolean test3 = doPredicate(x -> x > 0, 2);
boolean test4 = doPredicate(x -> x > 0, -1);
log.info("test3:{} test4:{}", test3, test4);
TestBean bean = new TestBean(2);
boolean test5 = doPredicate(TestBean::check, bean);
bean = new TestBean(-1);
boolean test6 = doPredicate(TestBean::check, bean);
log.info("test5:{} test6:{}", test5, test6);
}
private <T> boolean doPredicate(Predicate<T> predicate, T t) {
return predicate.test(t);
}
/*************************************
*
* BinaryOperator
*
*************************************/
@Test
public void do_BinaryOperator() {
// 二元的操作,入参由泛型指定,参数个数固定
// 泛型不指定,编译不通过
BinaryOperator<Long> operator = (x, y) -> x + y;
Long apply = operator.apply(1L, 2L);
log.info("operator:{}", apply);
}
/*************************************
*
* Function
*
*************************************/
@Test
public void do_Function() {
// 方法调用T 参数类型 R 返回值类型
// 无法指定方法入参,纯粹对象方法调用
Function<String, String> fun = a -> "Hello" + a;
String apply = fun.apply("123");
log.info("apply:" + apply);
TestBean bean = new TestBean(3);
Boolean doFunction = doFunction(TestBean::check, bean);
log.info("TestBean::check => " + doFunction);
}
private <A, B> B doFunction(Function<A, B> fun, A a) {
return fun.apply(a);
}
/*************************************
*
* BiFunction
*
*************************************/
@Test
public void do_BiFunction() {
BiFunction<String, String, String> fun = (a, b) -> "Hello a:" + a + " b:" + b;
String apply = fun.apply("abc", "123");
log.info("apply:\t" + apply);
BiFunction<TestBean, String, String> beanFun = TestBean::getMsg;
String apply2 = beanFun.apply(new TestBean(5), "abc");
log.info("apply2:\t" + apply2);
String apply3 = doBiFunction(TestBean::getMsg, new TestBean(8), "123");
log.info("apply3:\t" + apply3);
}
private <A, B, C> C doBiFunction(BiFunction<A, B, C> fun, A a, B b) {
return fun.apply(a, b);
}
/*************************************
*
* BiConsumer
*
*************************************/
@Test
public void do_BiConsumer() {
// 接受两个参数,返回空
BiConsumer<String, String> biConsumer = (a, b) -> {
System.out.println("a: " + a + " b:" + b);
};
biConsumer.accept("abc", "123");
BiConsumer<TestBean, String> consumer = TestBean::showMsg;
// 调用TestBean的showMsg 使用新增的TestBean实例 入参 abc
consumer.accept(new TestBean(), "abc");
// 调用TestBean的showMsg 使用新增的TestBean实例 入参 123
doBiConsumer(TestBean::showMsg, new TestBean(), "123");
}
private <A, B> void doBiConsumer(BiConsumer<A, B> consumer, A a, B b) {
consumer.accept(a, b);
}
}
个人理解,仅供参考,有问题欢迎留言 ^_^!