Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no BooleanConsumer in Java 8?

I'm afraid that this is somewhat a silly question.

Is there anybody can tell me why there is no BooleanConsumer opposite to BooleanSupplier?

Is there any reason other than "because simply there isn't"?

Should I create my own one? Or am I missing something else?

public interface BooleanConsumer {      void accept(boolean value);      default BooleanConsumer andThen(final BooleanConsumer after) {         return v -> {             accept(v);             after.accept(v);         }     } } 

Update

Where to use? I'm writing a library that uses much of consumers and suppliers. I successfully wrote a line with LongConsumer and I encountered a situation that expecting a consumer accepting a boolean value which is from a method result. Say Files.deleteIfExist?

like image 889
Jin Kwon Avatar asked Dec 30 '14 02:12

Jin Kwon


People also ask

Why do we need a Consumer in Java 8?

Consumer<T> is an in-built functional interface introduced in Java 8 in the java. util. function package. Consumer can be used in all contexts where an object needs to be consumed,i.e. taken as input, and some operation is to be performed on the object without returning any result.

What is the purpose of BooleanSupplier function in Java 8?

The BooleanSupplier Interface is a part of the java. util. function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which does not take in any argument but produces a boolean value.

What is Consumer <> in Java?

Java Consumer is a functional interface which represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.


2 Answers

IntConsumer and LongConsumer are needed to avoid the overhead autoboxing every value. It is much more efficent to be working on raw primitives. However, for Boolean and Byte every possible object is cached so there is little reason to avoid using Consumer<Boolean> or Consumer<Byte>

like image 62
Peter Lawrey Avatar answered Oct 07 '22 19:10

Peter Lawrey


As other answers indicate there is no great reason to avoid Consumer<Boolean>, but then there's no great reason to avoid Supplier<Boolean> either, so a different explanation is required for this.

A similar question is why can't you switch on a boolean value. The answer is that there's no need because you could always use if or if else.

A BooleanConsumer would really be nothing more than an if else construct because the accept() method for a BooleanConsumer could always be written in this form:

if (v) {     // Do something } else {     // Do something else } 

If you needed to pass such code around as data, you could just pass a pair of Runnables representing "do something" and "do something else". In many cases, you would only need one of the Runnables because one of the two blocks above would be empty.

In the same way, there is no need for a BooleanPredicate because it would be nothing more than a pair of BooleanSuppliers and there is no need for a a BooleanFunction<R> because it would be nothing more than a pair of Supplier<R>s.

In contrast to this, it is not possible to break a BooleanSupplier into two simpler objects.

like image 40
Paul Boddington Avatar answered Oct 07 '22 19:10

Paul Boddington