Java 8 primitive stream to collection mapping methods

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

    The second is better as it creates no Integer objects in this example.

    From the JLS 5.1.7

    If the value p being boxed is ... an int ... between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

    Generally speaking, you should not call new on wrapper types, but use their static factory methods - for example Integer.valueOf

提交回复
热议问题