IntStream 与通常的 Stream 相比有什么优势?

IntStreamDoubleStreamLongStream 如何优于 Java 8 中的常规流?

How is IntStream, DoubleStream, or LongStream better than regular stream in Java 8?

这些线程是否具有高性能或可用性?

Do these threads have high performance or maybe usability?

推荐答案

Stream<Integer> 等必须使用装箱值(Integer 而不是原始 int) 会占用更多内存,并且通常需要大量装箱/拆箱操作(取决于您的代码).为什么只有 Int/Double/Long?只是因为它们被期望最常使用.

Stream<Integer> etc. have to work with boxed values (Integer instead of primitive int) which takes significantly more memory and usually a lot of boxing/unboxing operations (depending on your code). Why only Int/Double/Long? Just because they were expected to be used most often.

同样适用于 OptionalInt 和朋友以及所有功能接口.

Same applies to OptionalInt and friends and all the functional interfaces.

对于集合(列表/地图/集合),出于同样的原因,有许多第三方库提供原始专业化.确实,问题更加严重,因为对于流,您不需要(通常;sorted() 是一个反例)需要在内存中存储许多值.

For collections (lists/maps/sets) there are many third-party libraries providing primitive specialization for the same reason. Really the problem there is even more acute because with streams you don't (usually; sorted() is a counter-example) need to store many values in memory.

相关文章