Java 8 primitive stream to collection mapping methods

后端 未结 6 964
醉话见心
醉话见心 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 11:07

    Yes, boxed() uses Integer.valueOf which can retrieve some Integer instances from a cache.

    So you should either use the version with boxed() (preferably), or use Integer.valueOf instead of new Integer(). Note that boxed() is in fact shorthand for mapToObj(Integer::valueOf).

提交回复
热议问题