What is the advantage of IntStream over usual Stream?

前端 未结 1 460
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 03:08

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

Do these threads have high performance or

相关标签:
1条回答
  • 2021-01-15 03:26

    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.

    0 讨论(0)
提交回复
热议问题