类型参数,进行处理后无任何返回值。
通俗的来说:
Consumer 相当于一个放东西的容器,你可以在这个容器里放有一个入参的代码块,然后返回 T 类型,当调用 accept(T) 方法的时候才会去执行容器里的代码。
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
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}]
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}]
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
BiConsumer和上面的Consumer非常相似,只不过BiConsumer可以接受两个泛型
类型参数,进行处理后无任何返回值。
import java.util.*;
import java.util.function.BiConsumer;
public class MainServer {
public static void main(String[] args) throws Exception {
List<Integer> lista = new ArrayList<>();
lista.add(2);
lista.add(1);
lista.add(3);
List<Integer> listb = new ArrayList<>();
listb.add(2);
listb.add(1);
listb.add(2);
// 第一个BiConsumer
BiConsumer<List<Integer>, List<Integer>> equals = (list1, list2) -> {
if (list1.size() != list2.size()) {
System.out.println("False");
} else {
for (int i = 0; i < list1.size(); i++)
if (list1.get(i) != list2.get(i)) {
System.out.println("False");
return;
}
System.out.println("True");
}
};
// 第二个BiConsumer
BiConsumer<List<Integer>, List<Integer> > disp = (list1, list2) -> {
list1.stream().forEach(a -> System.out.print(a + " "));
System.out.println();
list2.stream().forEach(a -> System.out.print(a + " "));
System.out.println();
};
System.out.println("开始执行:");
// 先执行equals再执行equals,接收的参数是lista和listb
equals.andThen(disp).accept(lista, listb);
}
}
输出:
开始执行:
False
2 1 3
2 1 2
Student类:
import java.util.List;
public class Student {
public String name;
public int score;
public List<Integer> sc;
public Student (String name ,int score,List<Integer> sc){
this.name=name;
this.score=score;
this.sc=sc;
}
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;
}
public List<Integer> getSc() {
return sc;
}
public void setSc(List<Integer> sc) {
this.sc = sc;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", score=" + score +
", sc=" + sc +
'}';
}
}
BiConsumerTest类:
import com.test.Student;
import java.util.function.BiConsumer;
import static java.util.Objects.requireNonNull;
public interface BiConsumerTest extends BiConsumer<Student, String> {
@Override
default BiConsumerTest andThen(final BiConsumer<? super Student, ? super String> after) {
requireNonNull(after, "after");
return (l, r) -> {
accept(l, r);
after.accept(l, r);
};
}
}
测试类:
import com.test.Student;
import com.test.BiConsumerTest;
import java.util.*;
public class MainServer {
public static void main(String[] args) throws Exception {
Student student1 = new Student("张三", 85, Arrays.asList(95, 87));
BiConsumerTest biConsumerTest1 = myBiConsumerTest1("姓名-");
BiConsumerTest biConsumerTest2 = myBiConsumerTest2("成绩-");
biConsumerTest1.andThen(biConsumerTest2).accept(student1, "序号1:");
}
static BiConsumerTest myBiConsumerTest1(String str) {
//(student, number) -> System.out.println(number + str + student.name)相当于accept方法
return (student, number) -> System.out.println(number + str + student.name);
}
static BiConsumerTest myBiConsumerTest2(String str) {
//(student, number) -> System.out.println(number + student.name + str + student.score);相当于accept方法
return (student, number) -> System.out.println(number + student.name + str + student.score);
}
}
输出:
序号1:姓名-张三
序号1:张三成绩-85
BiConsumer
DoubleConsumer 处理一个double类型的参数
IntConsumer 处理一个int类型的参数
LongConsumer 处理一个long类型的参数
ObjIntConsumer 处理两个参数,且第二个参数必须为int类型
ObjLongConsumer 处理两个参数,且第二个参数必须为long类型
参考:
About return lambda expressions