IntStream 与通常的 Stream 相比有什么优势? [英] What is the advantage of IntStream over usual Stream?

查看:28
本文介绍了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.

这篇关于IntStream 与通常的 Stream 相比有什么优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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