Consumer
/*
* java.util.function.Consumer<T>接口和supplier接口相反
* 它是消费一个数据,
* Consumer泛型执行什么类型,就用accept方法消费什么类型
*
* */
public class Demo1 {
public static void getConsumer(String str, Consumer<String> con){
con.accept(str);
}
public static void main(String[] args) {
getConsumer("hello,world",(name)->{
String str = new StringBuffer(name).reverse().toString();
System.out.println(str);
});
}
}
/*
* Consumer接口的默认方法andThen
* 作用:需要两个Consumer接口,可以把两个Consumer接口结合到一起,在对数据消费
* 例如:
* Consumer<String> con1
* Consumer<String> con2
* String s = "hello";
* con1.accept(s);
* con2.accept(s);
* 连接两个Consumer接口,再进行消费
* con1.andThen(con2).accept(s); 谁写前面先消费
* */
import java.util.function.Consumer;
public class Demo3 {
public static void getConsumer(String s, Consumer<String> con1,Consumer<String> con2){
con1.andThen(con2).accept(s);
}
public static void main(String[] args) {
getConsumer("hello",(s)->{
System.out.println(s.toUpperCase());
},(s -> {
System.out.println(s.toLowerCase());
}));
}
}
标签:con1,con2,String,accept,Consumer,public
From: https://www.cnblogs.com/-xyk/p/16786578.html