Java 8 primitive stream to collection mapping methods

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

    In terms of best practice, the second approach is preferable because when you want to box a primitive value boxed is created exactly for that purpose and does so in the most optimised way possible whereas mapToObj, although can be used to achieve the same thing, is not the idiomatic approach.

    Further, it's advised not to use the primitive wrapper type constructors anymore and instead favour the valueOf methods, this is because these constructors have been deprecated as well as the fact that by using the valueOf it reduces memory footprint compared to the constructors leading to better performance.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2021-01-13 11:05

    For both performance and best practices, we should choose boxed() over mapToObj((in) -> new Integer(in)).


    Performance:

    See below boxed() source code (in the abstract class IntPipeline<E_IN> which implements IntStream, in the package java.util.stream ):

    @Override
        public final Stream<Integer> boxed() {
            return mapToObj(Integer::valueOf);
        }
    

    So it is calling mapToObj() with Integer::valueOf. Note that by reusing cached values,Integer::valueOf is more efficient than new Integer() , so we should choose boxed().


    Best practice:

    Besides the performance difference, with boxed() we write less, and it is easier to read.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 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)

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