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.
Answer from Tunaki on Stack OverflowOracle
docs.oracle.com › javase › 8 › docs › api › java › util › stream › IntStream.html
IntStream (Java Platform SE 8 )
March 16, 2026 - Returns a Stream consisting of the elements of this stream, each boxed to an Integer.
GeeksforGeeks
geeksforgeeks.org › java › intstream-boxed-java
IntStream boxed() in Java - GeeksforGeeks
July 11, 2025 - Stream<Integer> stream1 = stream.boxed(); // Displaying the elements stream1.forEach(System.out::println); } } Output : ... // Java code for IntStream boxed() import java.util.*; import java.util.stream.Stream; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Creating an IntStream IntStream stream = IntStream.range(3, 8); // Creating a Stream of Integers // Using IntStream boxed() to return // a Stream consisting of the elements // of this stream, each boxed to an Integer.
TutorialsPoint
tutorialspoint.com › intstream-boxed-method-in-java
IntStream boxed() method in Java
July 30, 2019 - The boxed() method of the IntStream class returns a Stream consisting of the elements of this stream, each boxed to an Integer. The syntax is as follows. At first, create an IntStream Now, use the boxed() method to return a Stream consisting of the
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › stream › IntStream.html
IntStream (Java SE 11 & JDK 11 )
January 20, 2026 - Returns a Stream consisting of the elements of this stream, each boxed to an Integer. This is an intermediate operation. ... Returns a builder for an IntStream.
Tabnine
tabnine.com › home page › code › java › java.util.stream.intstream
java.util.stream.IntStream.boxed java code examples | Tabnine
*/ protected ArrayList<TreeRoot> initTrees(Queue<TreeNode> treesQueue) { assert featuresPerTree > 0; ArrayList<TreeRoot> roots = new ArrayList<>(); List<Integer> allFeatureIds = IntStream.range(0, meta.size()).boxed().collect(Collectors.toList()); for (TreeNode node : treesQueue) { Collections.shuffle(allFeatureIds, random); Set<Integer> featuresSubspace = allFeatureIds.stream() .limit(featuresPerTree).collect(Collectors.toSet()); roots.add(new TreeRoot(node, featuresSubspace)); } return roots; }
Blogger
java8example.blogspot.com › 2019 › 08 › java-8-intstream-boxed.html
Java 8 IntStream boxed() Method Example Java8Example
In this tutorial, We'll learn about the new java 8 IntStream API boxed() method. IntStream boxed() returns a Stream consisting of the elements of this stream, each boxed to an Integer. That means to convert all primitive int values to wrapper Integer type objects Stream.
Java2s
java2s.com › Tutorials › Java › java.util.stream › IntStream › IntStream.boxed_.htm
Java Streams - IntStream boxed() example
j ava 2s . c o m*/ public class Main { public static void main(String[] args) { IntStream i = IntStream.of(1,2,3,4,5,6,7); Stream< Integer> o = i.boxed(); o.forEach(System.out::println); } }
Netjstech
netjstech.com › 2022 › 01 › java-stream-boxed-with-examples.html
Java Stream - boxed() With Examples | Tech Tutorials
In Java stream API there are primitive specializations of Stream named IntStream, LongStream and DoubleStream and each of these interfaces has a boxed() method that returns a Stream consisting of the elements of this stream, each boxed to an Integer, Long or Double respectively.
Oracle
docs.oracle.com › en › java › javase › 26 › docs › api › java.base › java › util › stream › IntStream.html
IntStream (Java SE 26 & JDK 26)
March 16, 2026 - Returns a Stream consisting of the elements of this stream, each boxed to an Integer. ... Returns a builder for an IntStream.
Oracle
docs.oracle.com › javase › 9 › docs › api › java › util › stream › IntStream.html
IntStream (Java SE 9 & JDK 9 )
Returns a Stream consisting of the elements of this stream, each boxed to an Integer.
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › stream › IntStream.html
IntStream (Java SE 21 & JDK 21)
January 20, 2026 - Returns a Stream consisting of the elements of this stream, each boxed to an Integer. ... Returns a builder for an IntStream.
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › util › stream › IntStream.html
IntStream (Java SE 17 & JDK 17)
January 20, 2026 - Returns a Stream consisting of the elements of this stream, each boxed to an Integer. ... Returns a builder for an IntStream.
LinkedIn
linkedin.com › pulse › demystifying-boxed-method-java-stream-api-shreyansh-pandey-vr6nc
Demystifying the Boxed Method in Java Stream API
We cannot provide a description for this page right now