Home >Java >javaTutorial >How do I Convert an int[] to an Integer[] for Use as a Map Key in Java?
In Java, Map does not natively support primitive types like int[] as keys. To address this issue, you can convert your int[] to Integer[] before adding them as keys to a Map
One efficient method for conversion using Java 8 is the stream() method. Here's how you can implement it:
<code class="java">int[] q = {1, 2, 3, 4}; Integer[] convertedQ = Arrays.stream(q).boxed().toArray(Integer[]::new);</code>
By using the boxed() method on the IntStream, you transform each int into an Integer, and the toArray(Integer[]::new) part creates a new Integer[] array with the converted values.
This approach provides a concise and native solution for converting int[] to Integer[] in Java, allowing you to store the converted array as keys in your Map.
The above is the detailed content of How do I Convert an int[] to an Integer[] for Use as a Map Key in Java?. For more information, please follow other related articles on the PHP Chinese website!