就是Java 8缺少OptionalBoolean? [英] Is Java 8 missing an OptionalBoolean?

查看:590
本文介绍了就是Java 8缺少OptionalBoolean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可选 *,爪哇1.8提供<一个href=\"http://docs.oracle.com/javase/8/docs/api/java/util/OptionalInt.html\"><$c$c>OptionalInt, <一href=\"http://docs.oracle.com/javase/8/docs/api/java/util/OptionalLong.html\"><$c$c>OptionalLong和<一个href=\"http://docs.oracle.com/javase/8/docs/api/java/util/OptionalDouble.html\"><$c$c>OptionalDouble.

但我不能找到相当于 OptionalBoolean 类。

But I cannot find the equivalent OptionalBoolean class.

是否有对任何有技术原因的 OptionalBoolean

Are there any technical reasons against having an OptionalBoolean?

*
可选可能会或可能不会有一个价值presence,用作替代

* An Optional may or may not have the presence of a value, is used as an alternative to null.

推荐答案

这句话解释了其原始流背后的考虑。我假设应用到基本选配相同。总之,原始数据流(也可能是选配的孔)出于性能方面的创建。他们没有创造他们所有8个基本类型,以减少code重复和界面的污染。

This quote explains the considerations behind having primitive streams. I'm assuming the same applied to primitive Optionals. In short, primitive streams (and probably Optionals as well) were created for performance reasons. They didn't create them for all 8 primitive types to reduce code duplication and interface pollution.

引用作者Brian Goetz的话在拉姆达邮件列表

Quoting the words of Brian Goetz in the lambda mailing list:

更普遍的:背后有专门的经营理念
  原始流(例如,IntStream)充满了讨厌的权衡。
  在一方面,它的很多丑陋的code复制,界面
  污染等。另一方面,在盒装OPS任何一种算术
  太烂了,又没有什么故事,减少了整数将是可怕的。
  因此,我们正处在一个艰难的角落,而我们正在努力不使情况变得更糟。

More generally: the philosophy behind having specialized primitive streams (e.g., IntStream) is fraught with nasty tradeoffs. On the one hand, it's lots of ugly code duplication, interface pollution, etc. On the other hand, any kind of arithmetic on boxed ops sucks, and having no story for reducing over ints would be terrible. So we're in a tough corner, and we're trying to not make it worse.

招数不使情况变得更糟#1:我们没有做所有八个
  基本类型。我们正在做的int,long和double;所有其他
  可以通过这些模拟。可以说,我们可以摆脱整型过,但
  我们不认为大多数Java开发人员都准备好了。是的,有
  将是性格电话,得到的答复是坚持在一个int。
  (每个专业化预计〜100K到JRE的足迹。)

Trick #1 for not making it worse is: we're not doing all eight primitive types. We're doing int, long, and double; all the others could be simulated by these. Arguably we could get rid of int too, but we don't think most Java developers are ready for that. Yes, there will be calls for Character, and the answer is "stick it in an int." (Each specialization is projected to ~100K to the JRE footprint.)

诀窍#2:我们正在使用原始数据流揭露的事情是最好的原始域做(整理,还原),但不
  试图复制一切你可以在盒装域做的。对于
  例如,有没有IntStream.into(),如阿列克谢指出。 (如果有
  中,下一个问题(S)将是哪里是IntCollection?
  IntArrayList? IntConcurrentSkipListMap?)我们的目的是多流
  开始可能是参考流,并最终成为原始数据流,但
  而不是相反。这是确定的,以及减少转换次数
  需要(例如,没有找到相关地图为int的过载 - > T,没有专业化
  对于int功能 - > T等)

Trick #2 is: we're using primitive streams to expose things that are best done in the primitive domain (sorting, reduction) but not trying to duplicate everything you can do in the boxed domain. For example, there's no IntStream.into(), as Aleksey points out. (If there were, the next question(s) would be "Where is IntCollection? IntArrayList? IntConcurrentSkipListMap?) The intention is many streams may start as reference streams and end up as primitive streams, but not vice versa. That's OK, and that reduces the number of conversions needed (e.g., no overload of map for int -> T, no specialization of Function for int -> T, etc.)

和我要指出,我找到了答案报价为<一个href=\"http://stackoverflow.com/questions/22918847/why-are-new-java-util-arrays-methods-in-java-8-not-overloaded-for-all-the-primit\">this问题。

And I should mention that I found that quote in the answer to this question.

这篇关于就是Java 8缺少OptionalBoolean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆