Java的lambda实战(三)如何让自己的类支持lambda

1、背景

前两章讲了lambda基础以及常用到lambda的场景,本章讲函数式接口的接口。
再自己代码中使用这些接口后,可以让自己的代码支持lambda。

2、函数式接口实例

接收一个参数,返回一个参数

2.1、Function

/**
 * Function 接收一个参数,返回一个参数
 */
public static void functionDemo() {
	Function<Integer, Integer> f1 = t -> t * t;
	Function<Integer, Integer> f2 = t -> t * 2;

	// apply 执行Function,返回执行结果
	System.out.println("Function:apply");
	System.out.println(f1.apply(3));

	// compose 在 f1 之前执行 f2 : (3 * 2) * (3 * 2) = 36
	System.out.println("Function:compose");
	System.out.println(f1.compose(f2).apply(3));

	// andThen 在 f1 之后执行 f2 : 3 * 3 * 2 = 18
	System.out.println("Function:andThen");
	System.out.println(f1.andThen(f2).apply(3));

	// identity 返回本身
	System.out.println(Function.identity().apply("Function:identity"));

	// compose 和 andThen 一起使用
	System.out.println("Function:compose & andThen");
	// compose(f2) & f1.apply() & andThen(f2) : [(3 * 2) * (3 * 2)] * 2 = 72
	System.out.println(f1.compose(f2).andThen(f2).apply(3));
	// compose(f1) & compose(f2) & f1.apply() & andThen(f2) : {[(5 * 5) * 2] * [(5 * 5) * 2]} * 2 = 5000
	System.out.println(f1.compose(f2).andThen(f2).compose(f1).apply(5));
	// compose(f2) & compose(f1) & f1.apply() & andThen(f2) : {[(5 * 2) * (5 * 2)] * [(5 * 2) * (5 * 2)]} * 2 = 20000
	System.out.println(f1.compose(f1).andThen(f2).compose(f2).apply(5));
	// compose(f2) & f1.apply() & andThen(f1) & andThen(f2) : {[(5 * 2) * (5 * 2)] * 2} * {[(5 * 2) * (5 * 2)] * 2} = 40000
	System.out.println(f1.compose(f2).andThen(f2).andThen(f1).apply(5));
}

compose 和 andThen 同时使用时:

  • 先执行 compose 再执行 andThen。
  • 如果有多个 compose ,从后往前执行。
  • 如果有多个 andThen ,从后往前执行。

2.2、Predicate

接收一个参数,返回boolean

/**
 * Predicate 接收一个参数,返回boolean
 */
public static void predicateDemo() {
	Predicate<Integer> p1 = t -> t % 4 == 0;
	Predicate<Integer> p2 = t -> t % 100 != 0;

	// test 执行Predicate,返回执行结果
	System.out.println("Predicate:test");
	System.out.println(p1.test(2020));

	// and p1 && p2
	System.out.println("Predicate:and");
	System.out.println(p1.and(p2).test(2020));

	// negate 否
	System.out.println("Predicate:negate");
	System.out.println(p1.negate().test(2020));

	// or p1 || p2
	System.out.println("Predicate:or");
	System.out.println(p1.or(p2).test(2020));

	// isEqual
	System.out.println("Predicate:isEqual");
	Predicate<Integer> p = Predicate.isEqual(2020);
	System.out.println(p.test(2020));
}

2.3、Supplier

不接收参数,返回一个参数

/**
 * Supplier 不接收参数,返回一个参数
 */
public static void supplierDemo() {
	Supplier<String> s1 = () -> "Supplier";

	// get 返回固定信息
	System.out.println("Supplier:get");
	System.out.println(s1.get());
}

2.4、Consumer

接收一个参数,不返回

/**
 * Consumer 接收一个参数,不返回
 */
public static void consumerDemo() {
	Consumer<String> c = System.out::println;

	// accept 执行Consumer
	System.out.println("Consumer:accept");
	c.accept("accept");

	// andThen
	System.out.println("Consumer:andThen");
	c.andThen(c).accept("andThen");
}

2.5、BiFunction

接收两个参数,返回一个参数

/**
 * BiFunction 接收两个参数,返回一个参数
 */
public static void biFunctionDemo() {
	BiFunction<String, String, String> b = (t, s) -> t + s;
	Function<String, String> f = t -> t + "apply";

	// accept 执行Consumer
	System.out.println(b.apply("BiFunction:", "apply"));

	// andThen b的返回值传入f的输入
	System.out.println(b.andThen(f).apply("BiFunction:", "andThen:"));
}

3、自定义实现

3.1、自定义函数式接口

@FunctionalInterface
public interface TrFunction<T, U, S, R> {
    R apply(T t, U u, S s);
}

3.2、调用自定义函数式接口

/**
 * TrFunction 接收三个参数,返回一个参数
 * 自定义的函数接口
 */
public static void trFunctionDemo() {
	TrFunction<String, String, String, String> tr = (t, u, s) -> t + u + s;
	System.out.println(tr.apply("123", "456", "789"));
}

3.3、自定义方法

自定义方法,可以让自己的代码支持 lambda 方法。

/**
 * 自定义方法
 * @param obj
 * @param function
 * @return
 */
public static Object reduce(Object obj, Function<Object, Object> function) {
	return function.apply(obj);
}

4、总结

通过这三章的梳理,这两周再我的代码中,lambda 的使用逐渐多了起来。加油!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值