为什么Java 8中没有BooleanConsumer? [英] Why there is no BooleanConsumer in Java 8?
问题描述
有没有人可以告诉我为什么不存在 BooleanConsumer
与 BooleanSupplier $相反c $ c>
?
除了因为根本就没有之外,还有什么原因吗?
我应该自己创建一个吗?或者我缺少其他的东西?
public interface BooleanConsumer {
void accept(boolean value);
default BooleanConsumer andThen(final BooleanConsumer after){
return v - > {
accept(v);
after.accept(v);
$ b $ / code $ / pre
$ b 更新
在哪里使用?我正在写一个使用大部分消费者和供应商的图书馆。我成功地写了一行 LongConsumer
,我遇到了一种情况,期望消费者接受来自方法结果的布尔值。说 Files.deleteIfExist
?
解决方案 IntConsumer
和 LongConsumer $需要c $ c>来避免自动装箱每个值的开销。在原始基元上工作更有效率。
但是,对于Boolean和Byte,所有可能的对象都被缓存,所以没有必要避免使用 Consumer< Boolean>
或 Consumer< Byte> ;
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
?
解决方案 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>
这篇关于为什么Java 8中没有BooleanConsumer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!