How to Convert Int[] into List≪Integer≫ in Java

How to convert int[] into List<Integer> in Java?

There is no shortcut for converting from int[] to List<Integer> as Arrays.asList does not deal with boxing and will just create a List<int[]> which is not what you want. You have to make a utility method.

int[] ints = {1, 2, 3};
List<Integer> intList = new ArrayList<Integer>(ints.length);
for (int i : ints)
{
intList.add(i);
}

Convert int[] into ArrayList

Use Arrays#asList(T... a) to create "a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)"

Integer[] intArray = {1, 2, 3, 42}; // cannot use int[] here
List<Integer> intList = Arrays.asList(intArray);

Alternatively, to decouple the two data structures:

List<Integer> intList = new ArrayList<Integer>(intArray.length);

for (int i=0; i<intArray.length; i++)
{
intList.add(intArray[i]);
}

Or even more tersely:

List<Integer> intList = new ArrayList<Integer>(Arrays.asList(intArray));

Converting from int array to Integer arraylist in java

An int[] is quite different from a List<Integer>. For example, an Integer has an identity as well as a value. There is no very simple way to do the conversion.

The following way works with Java 8.

int[] array = {1, 2, 3, 4, 5};
List<Integer> list = IntStream.of(array).boxed().collect(Collectors.toCollection(ArrayList::new));

The following way works in earlier versions.

int[] array = {1, 2, 3, 4, 5};
List<Integer> list = new ArrayList<Integer>();
for (int a : array)
list.add(a);

If you pass an int[] to Arrays.asList you get a List<int[]>, not a List<Integer>.

How can I convert List<Integer> to int[] in Java?

Unfortunately, I don't believe there really is a better way of doing this due to the nature of Java's handling of primitive types, boxing, arrays and generics. In particular:

  • List<T>.toArray won't work because there's no conversion from Integer to int
  • You can't use int as a type argument for generics, so it would have to be an int-specific method (or one which used reflection to do nasty trickery).

I believe there are libraries which have autogenerated versions of this kind of method for all the primitive types (i.e. there's a template which is copied for each type). It's ugly, but that's the way it is I'm afraid :(

Even though the Arrays class came out before generics arrived in Java, it would still have to include all the horrible overloads if it were introduced today (assuming you want to use primitive arrays).

Convert List<List<Integer>> to int[][] in Java 8

You can map to int the inner lists and collect them into int arrays, then collect the outer list as a 2D array:

int[][] flat = arr.stream()
.map(a -> a.stream().mapToInt(Integer::intValue).toArray())
.toArray(int[][]::new);

Convert List<String> to List<Integer> for only valid Integer

In order to remove all non-numeric characters from each string, you need to apply map() before converting the stream of String into stream of Integer.

List<Integer> nums =
strList.stream()
.map(s -> s.replaceAll("[^\\d]", ""))
.map(Integer::valueOf)
.collect(Collectors.toList());

System.out.println(nums);

Output for input {"464 mm", "80 mm", "1000", "1220", "48 mm", "1020"}

[464, 80, 1000, 1220, 48, 1020]

Note:

  • trim() after replaceAll() is redundant.
  • Since you are adding all the contents of the list generated by the stream pipeline into another list, you might substitute collect(Collectors.toList()) with toList() which is accessible with Java 16 onwards. The difference is that toList() returns an unmodifiable list and it more performant than collector.
  • Iterative solutions almost always perform better than stream-based. Lambdas and streams were introduced in Java to provide a way of organizing the code that more readable and concise.


Related Topics



Leave a reply



Submit