java OptionalInt 与 Optional<Integer>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33190768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OptionalInt vs Optional<Integer>
提问by Michael
When scrolling through the documentation for the java.utilpackage, I was surpised to find that Optional<T>
and OptionalInt
have no relationship to each other. This seems very hard to belive, as it suggests that they are unrelated classes.
当通过对文档滚动的java.util包,我surpised发现Optional<T>
并OptionalInt
具有彼此没有关系。这似乎很难相信,因为这表明它们是不相关的类。
- Why don't they have a common interface, class, are sub-types, or somethingto reveal the relationship they have? (They're verysimilar classes when you look at their uses.)
- Also, why the need for an additional
OptionalInt
class? Why can't you just useOptional<Integer>
? I thought it was due to the fact thatint
is primitive, but there is noOptionalChar
so that would be an inconsistent design choice.
- 为什么它们没有通用的接口、类、子类型或其他东西来揭示它们之间的关系?(当您查看它们的用途时,它们是非常相似的类。)
- 另外,为什么需要额外的
OptionalInt
课程?为什么不能直接使用Optional<Integer>
?我认为这是因为它int
是原始的,但没有OptionalChar
这样的设计选择是不一致的。
采纳答案by zapl
Java 8 introduced a whole lot dedicated to primitives. The reason is most likely that boxing primitives can create a lot of waste "boxes".
Java 8 引入了很多专用于原语的内容。原因很可能是拳击原语会产生大量的浪费“盒子”。
For example this
例如这个
OptionalInt optionalFirst = IntStream
.range(0, 100)
.filter(i -> i % 23 > 7)
.findFirst();
Here, an Optional<Integer>
as result would be inconsistent. Also methods like ifPresent(IntConsumer consumer)
then allow to stay withing the IntStream
world. Optional<Integer>
would force you to convert (which you can do easily if you want)
在这里,Optional<Integer>
结果将是不一致的。还有像ifPresent(IntConsumer consumer)
then 这样的方法允许与IntStream
世界保持联系。Optional<Integer>
会强迫你转换(如果你愿意,你可以很容易地做到)
There is no need for special support for char
or short
or byte
because all of them can be represented as int
. The missing one is boolean
but there is not much you can do in a stream with them since there are only 2 values.
不需要特别支持char
orshort
或byte
因为它们都可以表示为int
. 缺少的一个是,boolean
但是在与它们的流中您可以做的事情不多,因为只有 2 个值。
回答by Justin
There needs to be an OptionalInt
class for Java 8's streams to be consistent. If you take a look at the Stream
class, you'll see that manyofthemethodsreturn Optional<T>
. However, dealing with a Stream<Integer>
, Stream<Long>
or any other streams of primitives is exhausting, so there is an IntStream
class and a LongStream
class which replace the object with its unboxed value. For instance, finding the sum of the elements of a Stream<Integer>
is not trivial, whereas for an IntStream
, you just call IntStream#sum
OptionalInt
Java 8 的流需要有一个类才能保持一致。如果你在看看Stream
类,你会看到很多的的方法回报Optional<T>
。然而,处理一个Stream<Integer>
,Stream<Long>
或基元的任何其他流被排出,所以有一个IntStream
类和LongStream
类替换其未装箱值的对象。例如,找到 a 的元素之和Stream<Integer>
并非易事,而对于 an IntStream
,您只需调用IntStream#sum
In these classes, the JDK helpfully replaces Optional<T>
with OptionalInt
, OptionalLong
, and so forth.
在这些类中,JDK 有用地替换Optional<T>
为OptionalInt
、OptionalLong
等。
回答by Jiri Tousek
OptionalInt
is a container holding value of primitive type int
. Primitive types cannot be used as type parameters for generic classes, so there is no way it could be a subclass of Optional<T>
.
OptionalInt
是一个保存原始类型值的容器int
。原始类型不能用作泛型类的类型参数,因此它不可能是Optional<T>
.