piyushGoyal
piyushGoyal

Reputation: 1099

IntStream.boxed() vs for loop | Performance

I was writing a piece of code where I had a String[] and a method which takes this String[] and returns Byte[] maintaining the string-Byte pair with the position where few of the Byte could be null. Eventually, I have to transform the Byte and get a map of with key as string from String[] and value as the return of transformation. This is how I implemented the same in Java 8 streams:

IntStream.range(0, productReferences.length)
            .filter(index -> (null!= productsPrice[index])).boxed()
            .collect(Collectors.toMap(position -> productReferences[position],
                    position ->callSomeMethod(productsPrice[position])));

where productReference is String[] and productsPrice[] is Byte[] array.

Now the question is IntStream.boxed() method. Internally, it boxes int to Integer so that it returns a Stream which I believe is a costlier operation.

Other way would be use java for loop

    for(int i=0;i<productReferences.length; i++){
    if (productsPrice[index]==null) continue;
    //other code
}

What is the best way of handling such kind of scenario? I understand the reason to create IntStream but if I can actually have the index in collect method without boxed() method thus avoiding the boxing?

Upvotes: 3

Views: 2825

Answers (1)

Tunaki
Tunaki

Reputation: 137084

You can use the collect operation that you have on IntStream instead of boxing it into a Stream<Integer>.

IntStream.range(0, productReferences.length)
         .filter(index -> productsPrice[index] != null)
         .collect(
            HashMap::new,
            (m, i) -> m.put(productReferences[i], callSomeMethod(productsPrice[i])),
            Map::putAll
         );

This won't box every index into an Integer since the consumer part of the collector takes an ObjIntConsumer; so i in the code above is an int. As Holger noted, the initial code, using Collectors.toMap would throw an exception in the case of duplicate keys, when this version would overwrite the value.

You would still need to benchmark the two solutions on your real data to see if that brings an improvement.

Upvotes: 6

Related Questions