Java 8 primitive stream to collection mapping methods

后端 未结 6 958
醉话见心
醉话见心 2021-01-13 10:22

Is there any significant difference (in performance or best practices) between those two stream creation methods?

int[] arr2 = {1,2,3,4,5,6};

Arrays.stream(         


        
6条回答
  •  再見小時候
    2021-01-13 10:56

    IntStream.boxed() uses Integer.valueOf(int) which is an optimised version* of Integer(int).

    Internally, boxed() is defined as mapToObj(Integer::valueOf).


    *this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

    The Javadoc of Integer.valueOf(int)

提交回复
热议问题