Consumer Functional Interface

less than 1 minute read

Consumer

(used with forEach) takes generified argument and returns nothing

It is a function that represents side effects

public interface Consumer<T> {
    void accept(T t);
}

Consumer Example

List<String> strList = Arrays.asList("test","this","is","a","test","this","test","is","not","complex");

Consumer<String> c = s -> System.out.print(s + " ,");
//Consumer<String> c = System.out::println;
strList.stream().forEach(c);

BiConsumer example.

Using BiConsumer to insert values into hashmap.

Parenthesis Checker using Consumer

Tags:

Categories:

Updated: