Java 8 中的
Consumer 是一个函数接口,它可以接受一个泛型
<T> 类型参数,进行处理后无任何返回值。
accept(T) 方法是 Consumer 函数式接口的函数方法,传入一个任意类型,无返回值,可以用于 Lambda 表达式和方法引用。andThen(Consumer) 方法可以传入一个 Consumer ,返回组合了两个 Consumer 后的 Consumer ,传入的 Consumer 不能为 null ,否则会得到 NullPointerException 。
例如传入一个字符串,打印一个字符串。
通俗的来说Consumer相当于一个放东西的容器,你可以在这个容器里放有一个入参的代码块,然后返回T类型,当调用accept(T)方法的时候才会去执行容器里的代码。
一、consumer使用
示例代码1:
import java.util.function.Consumer;
public class MainServer {
public static void main(String[] args) throws InterruptedException {
Consumer<String> first = x -> System.out.println("1."+x.toLowerCase());
Consumer<String> second = y -> System.out.println("2." + y);
System.out.println("开始");
Consumer<String> result = first.andThen(second);
result.accept("A");
}
}
输出:
开始
1.a
2.A
示例代码2:
import com.alibaba.fastjson.JSON;
import com.Student;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
public class MainServer {
public static void main(String[] args) {
List<Student> lisiList = new ArrayList<>();
Consumer <Student> consumer = x -> {
if (x.name.equals("aa")){
lisiList.add(x);
}
};
Stream.of(
new Student("aa",99),
new Student("bb",98),
new Student("cc",85),
new Student("dd",69),
new Student("ee",77),
new Student("ff",83),
new Student("aa",82)
).forEach(consumer);
System.out.println(JSON.toJSONString(lisiList));
}
}
输出:
[{"name":"aa","score":99},{"name":"aa","score":82}]
示例代码3:
import com.alibaba.fastjson.JSON;
import com.xu.tao.model.Student;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
public class MainServer {
public static void main(String[] args) {
List<Student> lisiList = new ArrayList<>();
Consumer <Student> consumer = x -> {
if (x.name.equals("aa")){
lisiList.add(x);
}
};
consumer = consumer.andThen(
x -> lisiList.removeIf(y -> y.score > 90)
);
Stream.of(
new Student("aa",99),
new Student("bb",98),
new Student("cc",85),
new Student("dd",69),
new Student("ee",77),
new Student("ff",83),
new Student("aa",82)
).forEach(consumer);
System.out.println(JSON.toJSONString(lisiList));
}
}
输出:
[{"name":"aa","score":82}]
示例代码4:
public class Student {
public String name;
public int score;
public Student (String name ,int score){
this.name=name;
this.score=score;
}
public Student (){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return name + ": " + score;
}
}
import com.xu.tao.model.Student;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class MainServer {
public static void main(String[] args) {
List<Student> students = Arrays.asList(
new Student( "John",5),
new Student("Jane",2),
new Student("Jack",3)
);
Consumer<Student> raiser = e -> {e.score = e.score * 2;};
raiseStudents(students, System.out::println);
System.out.println("开始andThen");
raiseStudents(students, raiser.andThen(System.out::println));
}
private static void raiseStudents(List<Student> employees, Consumer<Student> fx) {
for (Student e : employees) {
fx.accept(e);
}
}
}
输出;
John: 5
Jane: 2
Jack: 3
开始andThen
John: 10
Jane: 4
Jack: 6
二、Consumer相关的接口
-
BiConsumer<T, U> 处理一个两个参数 -
DoubleConsumer 处理一个double类型的参数 -
IntConsumer 处理一个int类型的参数 -
LongConsumer 处理一个long类型的参数 -
ObjIntConsumer 处理两个两个参数,且第二个参数必须为int类型 -
ObjLongConsumer 处理两个两个参数,且第二个参数必须为long类型
|