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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:19:32  来源:igfitidea点击:

OptionalInt vs Optional<Integer>

javagenericsjava-8language-design

提问by Michael

When scrolling through the documentation for the java.utilpackage, I was surpised to find that Optional<T>and OptionalInthave 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具有彼此没有关系。这似乎很难相信,因为这表明它们是不相关的类。

  1. 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.)
  2. Also, why the need for an additional OptionalIntclass? Why can't you just use Optional<Integer>? I thought it was due to the fact that intis primitive, but there is no OptionalCharso that would be an inconsistent design choice.
  1. 为什么它们没有通用的接口、类、子类型或其他东西来揭示它们之间的关系?(当您查看它们的用途时,它们是非常相似的类。)
  2. 另外,为什么需要额外的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 IntStreamworld. 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 charor shortor bytebecause all of them can be represented as int. The missing one is booleanbut there is not much you can do in a stream with them since there are only 2 values.

不需要特别支持charorshortbyte因为它们都可以表示为int. 缺少的一个是,boolean但是在与它们的流中您可以做的事情不多,因为只有 2 个值。

回答by Justin

There needs to be an OptionalIntclass for Java 8's streams to be consistent. If you take a look at the Streamclass, 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 IntStreamclass and a LongStreamclass 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

OptionalIntJava 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>OptionalIntOptionalLong等。

回答by Jiri Tousek

OptionalIntis 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>.