Consumer andThen returns a composed Consumer that performs, in sequence, for the current operation followed by the after operation.
andThen
has the following syntax.
default Consumer<T> andThen(Consumer<? super T> after)
The following example shows how to use andThen
.
import java.util.function.Consumer; public class Main { public static void main(String[] args) { Consumer<String> c = (x) -> System.out.println(x.toLowerCase()); c.andThen(c).accept("Java2s.com"); } }
The code above generates the following result.
The following code shows how to chain Consumer to chain actions.
/*from w w w .java2 s. c o m*/ import java.util.Arrays; import java.util.List; import java.util.function.Consumer; public class Main { public static void main(String[] args) { List<Student> students = Arrays.asList( new Student(1, 3, "John"), new Student(2, 4, "Jane"), new Student(3, 3,"Jack")); Consumer<Student> raiser = e -> { e.gpa = e.gpa * 1.1; }; raiseStudents(students, System.out::println); raiseStudents(students, raiser.andThen(System.out::println)); } private static void raiseStudents(List<Student> employees, Consumer<Student> fx) { for (Student e : employees) { fx.accept(e); } } } class Student { public int id; public double gpa; public String name; Student(int id, long g, String name) { this.id = id; this.gpa = g; this.name = name; } @Override public String toString() { return id + ">" + name + ": " + gpa; } }
The code above generates the following result.