Explain Codes LogoExplain Codes Logo

How to convert int

java
stream-api
autoboxing
performance
Nikita BarsukovbyNikita Barsukov·Aug 18, 2024
TLDR

Convert an int[] to List<Integer> in Java 8+ by streaming the array using Arrays.stream, boxing every int into Integer using boxed(), and collecting everything into a list with Collectors.toList():

int[] array = {1, 2, 3}; // Java, forget your array bias. List<Integer> list = Arrays.stream(array) .boxed() .collect(Collectors.toList());

Now you have your int[] array sounding less robotic in a List<Integer>.

Doing it the Stream API way

In Java 8+, using Arrays.stream() and boxed() are key for this transformation. The puberty journey of int to Integer is done via the boxed() function and they unite in a list with collect():

int[] array = {1, 2, 3}; // Freestyle the conversion as in an always original boy band. List<Integer> list = Arrays.stream(array) .boxed() .collect(Collectors.toList());

In Java 16+, Stream.toList() comes up as a simplified star taking the collector's place:

int[] array = {1, 2, 3}; // Even boy bands evolve! Let Stream.toList() shine. List<Integer> list = Arrays.stream(array) .boxed() .toList();

Other avenues to explore

Now let's journey back in time. Pre-Java 8 had you looping like an old-school DJ, and it worked just as well:

int[] array = {1, 2, 3}; List<Integer> list = new ArrayList<>(array.length); // In loop we trust! for (int i : array) { list.add(i); }

By the way, Google's Guava library plays the cool kid on the block, offering Ints.asList():

int[] array = {1, 2, 3}; // Guava: So fresh, so clean List<Integer> list = Ints.asList(array);

Mind you, it still brings an external dependency vibe to the party.

Watch out for fallen rocks

One tricky thing here: Arrays.asList() might make you believe it can do that, but it won't. It's not into boxing, and it treats primitive arrays like a weird exotic pet. It results in List<int[]>, and that's not what you want.

Other types of arrays

And it's not just ints having fun here! The pattern applies to all kinds of arrays. For String[] arrays, Arrays.asList() does the job jewellery box smoothly:

String[] strings = {"a", "b", "c"}; // Strings know how to party. List<String> stringList = Arrays.asList(strings);

For an even more advanced touch, consider AbstractList and anonymous classes. They provide a nifty way to access int[] elements directly. So, forget explicit iteration or wrapping.

Performance when dealing with big data

When given a roomful of arrays, you might want to balance efficiency. The looping dance can sometimes outperform the flashy Stream API for vast arrays. Novelty isn't always the answer. Always keep an eye on the content and context of your array when picking your conversion method.

Automation in Java

Java's been evolving and made some tasks smoother over time. Remember manually wrapping? That's retro now. Autoboxing is the modern way of converting int to Integer. Plus, the Stream API is more elegant and powerful with its methods like Stream.toList() available from Java 16+.

The art of flexibility

Nuance matters too! Parallel streams for big arrays and custom array utilities for specialized conversions might be the ninja moves you need. This is not just a conversion: it's a shapeshifting journey deserving creative solutions.