What is the advantage of IntStream over usual Stream?

最后都变了- 提交于 2021-01-19 06:39:07

问题


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

Do these threads have high performance or maybe usability?


回答1:


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.

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

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.



来源:https://stackoverflow.com/questions/54840920/what-is-the-advantage-of-intstream-over-usual-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!