↓ Skip to main content
Streams

Consumer

The utility package java.util.function has this functional interface Consumer.

Each functional interface (Annotated with @FunctionalInterface) conceptually has a single method (abstract) called the functional method to which the lambda expression’s parameter and return types are matched against. Functional interfaces often represent abstract concepts like functions, actions, or predicates.

Instances of functional interfaces can be created with lambda expressions, method references, or constructor references.

Functional interface provide target types for lambda expressions and method references. — Oracle 8 [Java Doc]

Instance methods

Modifier and TypeMethod and Description
voidaccept(T t) Performs this operation on the given argument.
default Consumer<T>andThen(Consumer<? super T> after) Returns a composed Consumer that performs, in sequence, this operation followed by the after operation.
Functional interfaces can provide a target type in multiple contexts - assignment context, method invocation, or cast context — Oracle 8 [Java Doc]

Examples
#

Let’s use this Consumer.accept method.

Performs this operation on the given argument.

accept(T t)

package me.samarthya;

import java.util.function.Consumer;

public class Main {

    public static void ExampleOne() {
        Consumer<String> myString = (x) -> System.out.println(x);
        myString.accept("Hello World!");
    }
    public static void main(String[] args) {
        ExampleOne();
    }
}
public class Main {

    public static void ExampleOne() {
        Consumer<String> myString = (x) -> System.out.println(x);
        myString.accept("Hello World!");
    }
    public static void ExampleTwo() {
        Consumer<List<String>> myList = (x) -> x.stream().sorted().forEach( e -> System.out.println(" Element: "+ e));
        List<String> dummyContents = new LinkedList<String>(){
            {
                add(" Hello");
                add(" World");
                add("!");
            }
        };

        myList.accept(dummyContents);
    }

    public static void main(String[] args) {
        System.out.println(" Ex1.");
        ExampleOne();
        System.out.println(" Ex2.");
        ExampleTwo();
    }
}

andThen(Consumer)
#

public static void ExampleThree() {
        Consumer<List<String>> toUpper = (x) -> x.replaceAll(String::toUpperCase);
        Consumer<List<String>> toJoin = (x) -> x.stream().map(e -> String.format("%s(%d) ", e, e.length())).forEach(e -> System.out.printf(e));

        List<String> dummyContents = new LinkedList<String>() {
            {
                add("Hello");
                add("World");
                add("!");
            }
        };

        toUpper.andThen(toJoin).accept(dummyContents);

    }

    public static void main(String[] args) {
        System.out.println(" Ex3.");
        ExampleThree();
    }

Outputs

Ex3.
HELLO(5) WORLD(5) !(1)

Helpers
#

Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

Nil Interface

·2 mins
An interface in Golang is a set of methods and a type. What is the purpose of Nil interface in GO-lang A nil interface value holds neither value nor concrete type.

PS: Process Status

·2 mins
MAN Pages are one source of truth for all curious queries. man ps Name ps - report a snapshot of the current processes. It is used to display instant information about the active processes in the system.

Linux Utils

·5 mins
Ping # I have used ping often but never to an extend that I could understand the complete range of options and power available at hand.