Consumer

Saurabh Sharma

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

  • https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html
  • https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html
  • Interface Predicate<T>

Leave a Reply

Your email address will not be published. Required fields are marked *