Java 8 primitive stream to collection mapping methods

后端 未结 6 963
醉话见心
醉话见心 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

    The second one will effectively use the jvm's integer cache, thus it will be better both in performance, as well as a best practice (do not instantiate boxed types, rather let the vm do that for you).

    You could imitate the same behavior if you changed the new Integer(in) with Integer.valueOf(in)

提交回复
热议问题