2
  • Callable<R> takes no arguments and returns R.

  • Runnable takes no arguments and returns void.

  • Function<T, R> takes an argument, T, and returns R.

What about a function that takes an argument and returns void? What is this analog called in Java?

4
  • i may be wrong on this but I'm pretty sure you can use void as a generic argument in Java. Commented Jul 18, 2018 at 14:14
  • 3
    Consumer<T> Commented Jul 18, 2018 at 14:16
  • @aeskreis java.lang.Void could be used for this. Commented Jul 18, 2018 at 14:16
  • void as a type param seems a bit hacky. I was looking for Consumer -- thanks. Commented Jul 18, 2018 at 14:19

3 Answers 3

3

It's Consumer<T>. It has one input and returns void with method Consumer::accept(T t).

Represents an operation that accepts a single input argument and returns no result.

There is its variation called BiConsumer<T, U> which turns 2 inputs into void.

Sign up to request clarification or add additional context in comments.

Comments

1

It's called Consumer, it's a function that takes an argument(or more), does something and doesn't return anything. For example, if you use a forEach:

exampleList.forEach(x -> System.out.println(x));

Comments

1

What you are looking for is the Consumer. It is a functional interface that takes a single argument and returns void.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.