Comsumer和BiConsumer方法:
首先看一下两个接口:几乎差不多,就是方法参数多一个.BiConsumer可以看做Consumer的增强吧!
Consumer的作用就是定义一个函数,然后对其进行消费处理,(accept方法);
而andThen方法相当于是组合两个方法,返回一个新的方法,是先对给定的参数进行定义的操作然后在执行after操作;
BiConsumer就是Consumer的加强版,处理两个参数,默认方法andThen也是一样的:写个测试:
@Test
public void test41() throws Exception {
Consumer<Integer> action1 = (x) -> {
System.out.println("对传进来的进行加1操作: " + (x + 1));
};
Consumer<Integer> action2 = (x) -> {
System.out.println("对传进来的进行减1操作: " + (x - 1));
};
Consumer<Integer> anction3 = action1.andThen(action2);
//先执行加法在执行减法
System.out.println("执行anction1");
action1.accept(3);
System.out.println("执行anction2");
action2.accept(3);
System.out.println("执行anction3");
anction3.accept(3);
}
@Test
public void test42() throws Exception {
BiConsumer<Integer, Integer> action1 = (x, y) -> {
System.out.println("对传进来的进行相加操作: " + (x + y));
};
BiConsumer<Integer, Integer> action2 = (x, y) -> {
System.out.println("对传进来的进行相减操作: " + (x - y));
};
BiConsumer<Integer, Integer> anction3 = action1.andThen(action2);
//先执行加法在执行减法
System.out.println("执行anction1");
action1.accept(1, 1);
System.out.println("执行anction2");
action2.accept(1, 1);
System.out.println("执行anction3");
anction3.accept(1, 1);
}
执行结果:
Function和BiFunction的区别
先看源码,其实感觉跟Consumer都差不多:
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of input to the {@code before} function, and to the
* composed function
* @param before the function to apply before this function is applied
* @return a composed function that first applies the {@code before}
* function and then applies this function
* @throws NullPointerException if before is null
*
* @see #andThen(Function)
*/
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
/**
* Returns a composed function that first applies this function to
* its input, and then applies the {@code after} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of output of the {@code after} function, and of the
* composed function
* @param after the function to apply after this function is applied
* @return a composed function that first applies this function and then
* applies the {@code after} function
* @throws NullPointerException if after is null
*
* @see #compose(Function)
*/
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
/**
* Returns a function that always returns its input argument.
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
static <T> Function<T, T> identity() {
return t -> t;
}
}
BiFunction的源码:
@FunctionalInterface
public interface BiFunction<T, U, R> {
/**
* Applies this function to the given arguments.
*
* @param t the first function argument
* @param u the second function argument
* @return the function result
*/
R apply(T t, U u);
/**
* Returns a composed function that first applies this function to
* its input, and then applies the {@code after} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of output of the {@code after} function, and of the
* composed function
* @param after the function to apply after this function is applied
* @return a composed function that first applies this function and then
* applies the {@code after} function
* @throws NullPointerException if after is null
*/
default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t, U u) -> after.apply(apply(t, u));
}
}
先说function,function主要就apply方法,接收参数为T返回R类型的;
compose,和andThen都是差不多的,都是组合成一个新的函数,然后按照不同顺序执行…
identity是一个静态方法,注释也写的很明了了,返回接收的参数的函数,就是接收啥返回对应的函数;
@Test
public void test51() throws Exception {
Function<Integer, Integer> action1 = (x) -> {
System.out.println("对传进来的进行加1操作: " + (x + 1));
return x + 1;
};
Function<Integer, Integer> action2 = (x) -> {
System.out.println("对传进来的进行-1操作: " + (x - 1));
return (x - 1);
};
Function<Integer, Integer> action3 = action1.andThen(action2);
Function<Integer, Integer> compose = action1.compose(action2);
//先执行加法在执行减法
System.out.println("执行anction1");
action1.apply(1);
System.out.println("执行anction2");
action2.apply(1);
System.out.println("执行andThen返回的函数");
action3.apply(1);
System.out.println("执行compose返回的函数");
compose.apply(1);
//Function.identity()返回一个输出跟输入一样的Lambda表达式对象,等价于形如t -> t形式的Lambda表达式
Stream<String> stream = Stream.of("I", "love", "you", "too");
//这三个相同的作用...就是返回的是原来的字符串.
// Map<String, Integer> map = stream.collect(Collectors.toMap(Function.identity(), String::length));
Map<String, Integer> map = stream.collect(Collectors.toMap(String::toString, String::length));
// Map<String, Integer> map = stream.collect(Collectors.toMap(str->str, String::length));
System.out.println(map);
}
执行结果:
BiFunction跟BiConsumer差不多,都是对原来的进行增强,多加了个参数,都是差不多的…注意BiFunction的andThen方法是传入的Function,
@Test
public void test52() throws Exception {
BiFunction<Integer, Integer, Integer> action = (x, y) -> {
System.out.println(x + y);
return x + y;
};
Function<Integer, Integer> action2 = (x) -> {
System.out.println("Function..." + x);
return x;
};
BiFunction<Integer, Integer, Integer> integerIntegerBiConsumer = action.andThen(action2);
integerIntegerBiConsumer.apply(1, 1);
}
fuction包下
有很多扩展的接口,比如DoubleConsumer,请求参数固定是dubbo,等…