Java SE API

Consumer.andThen()

Executes the first action and then after on normal completion. Earlier side effects are not rolled back.

Formal Syntax & Structure

java syntaxjava
default Consumer<T> andThen(Consumer<? super T> after)

Parameters & Attributes

NameTypeDescription
afterConsumer<? super T>Second non-null action

Return Value

Consumer<T>

Sequential action

Working Example

Example Snippetjava
import java.util.*;import java.util.function.*;public class Main {
    public static void main(String[] args) {        List<String> log = new ArrayList<>();        Consumer<String> first = s -> log.add("received " + s);        Consumer<String> reject = s -> { throw new IllegalArgumentException("invalid"); };        Consumer<String> last = s -> log.add("saved " + s);        try { first.andThen(reject).andThen(last).accept("A"); }        catch (IllegalArgumentException ex) { System.out.println(ex.getMessage()); }        System.out.println(log);    }}
Java Reference