逐步走向响应式编程(二)

编程入门 行业动态 更新时间:2024-10-10 01:21:48

逐步<a href=https://www.elefans.com/category/jswz/34/1764553.html style=走向响应式编程(二)"/>

逐步走向响应式编程(二)

java8提供的Consumer也是一大常用接口,其功能正如其名,用于“消费”操作。此接口提供了两个方法,其中一个是待实现的方法accept,另一个是默认方法andThen。此接口主要用于对某数据进行处理,无需返回值的情景。接下来从以下几个方面来认识此接口。

    • 源码解析
    • 案例分析
      • accept方法
      • andThen方法
      • 万能方法
    • 总结

源码解析

package java.util.function;import java.util.Objects;/*** Represents an operation that accepts a single input argument and returns no* result. Unlike most other functional interfaces, {@code Consumer} is expected* to operate via side-effects.** <p>This is a <a href="package-summary.html">functional interface</a>* whose functional method is {@link #accept(Object)}.** @param <T> the type of the input to the operation** @since 1.8*/
@FunctionalInterface
public interface Consumer<T> {/*** Performs this operation on the given argument.* 此方法为本接口的核心方法,用于实现消费的逻辑,参数为待消费的数据* @param t the input argument*/void accept(T t);/*** Returns a composed {@code Consumer} that performs, in sequence, this* operation followed by the {@code after} operation. If performing either* operation throws an exception, it is relayed to the caller of the* composed operation.  If performing this operation throws an exception,* the {@code after} operation will not be performed.** @param after the operation to perform after this operation* @return a composed {@code Consumer} that performs in sequence this* operation followed by the {@code after} operation* @throws NullPointerException if {@code after} is null* 此方法是将多个消费逻辑组合起来实现对同一组数据进行多次消费*/default Consumer<T> andThen(Consumer<? super T> after) {Objects.requireNonNull(after);return (T t) -> { accept(t); after.accept(t); };}
}

案例分析

本案例对一个int数组进行操作int num[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 另外需要借助一个决断函数checkData:

 private static <T> boolean checkData(T t, Predicate<T> predicate) {return predicate.test(t);}

accept方法

打印数组中的偶数值

//实现偶数值的消费逻辑
Consumer<int[]> evenConsumer = t -> {for (int i : t) {if (checkData(i, k -> k % 2 == 0)) {System.out.print("=" + i);}}};
//开始消费
evenConsumer.accept(num);
//最终结果
=2=4=6=8

andThen方法

同一组数据实现不同的处理方法,就需要使用此方法。例如在上面的基础之前还需要打印>2的数

//实现>2的值消费逻辑
Consumer<int[]> moreThan2 = t -> {for (int i : t) {if (checkData(i, k -> k > 2)) {System.out.println("++" + i);}}};
//开始消费        evenConsumer.andThen(moreThan2).accept(num);  
//最终结果
=2=4=6=8
++3++4++5++6++7++8++9 

需要注意的一点就是,andThen方法只是加了一种消费逻辑,第一次消费不会影响第二次消费

万能方法

本人在此用consumer的时候,通常用一个方法对消费逻辑和消费对象包装在一个方法中,方便重用

public static <T> void consumeData(T t, Consumer<T> consumer) {consumer.accept(t);}

上述两个例子可以调用此方法

//打印偶数值
类名.consumeData(num, evenConsumer);
//打印>2的值
类名.consumeData(num, moreThan2);
//两者都打印
类名.consumeData(num, evenConsumer.andThen(moreThan2));

另外java8 还提供了很多其他的Consumer接口,用法基本一致:

类名作用
BiConsumer用于同时消费两组数据,经常用于两组需要交互的数据
DoubleConsumer消费Double类型的数据
IntConsumer消费Int类型的数据
LongConsumer消费Long类型的数据
ObjDoubleConsumer用于同时消费两组数据,其中一种数据类型为Double,另一种为Object
ObjIntConsumer用于同时消费两组数据,其中一种数据类型为Int,另一种为Object
ObjLongConsumer用于同时消费两组数据,其中一种数据类型为Long,另一种为Object

总结

内容很简单,所有的消费最终都会交给accept处理,因为accept是唯一需要实现的接口,也就是说此方法实现了消费逻辑。在java8的Stream中,Consumer使用很广泛,后续的博文中会一一介绍。最后希望本文能帮助大家,祝大家在IT之路上少走弯路,一路绿灯不堵车,测试一性通过,bug秒解!

更多推荐

逐步走向响应式编程(二)

本文发布于:2024-02-10 22:32:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1677681.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:走向

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!