13

in the Android Java world, is there a straighforward (ideally one-call) way to convert an array of int to an ArrayList<Integer> and back? Calling toArray() on the ArrayList returns an array of Integer - not quite what I want.

I can easily do that by hand with a loop (already did, in fact). I'm wondering if the library/language supports the same.

EDIT: thanks all, I already wrote my own boxer and unboxer. I'm just surprised the language/RTL designers didn't think of it themselves, especially with primitive types being by design ineligible for collections.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • 2
    Core Java does not. I'm sure someone can provide a link to a 3rd party library that can do it for you. – jjnguy May 19 '11 at 20:32
  • possible duplicate of [How to convert int\[\] into List in Java?](http://stackoverflow.com/questions/1073919/how-to-convert-int-into-listinteger-in-java) – finnw May 19 '11 at 21:03
  • You might wanna have a look at this thread: https://stackoverflow.com/questions/44297189/how-to-collect-the-results-of-a-stream-in-a-primitive-array – Flavio Oliva Apr 16 '20 at 15:23

4 Answers4

13

Boxing and unboxing (without special libraries):

// Necessary Imports:
import java.util.*;
import java.util.stream.*;

// Integers:
int[] primitives = {1, 2, 3};
Integer[] boxed = Arrays.stream(primitives).boxed().toArray(Integer[]::new);
int[] unboxed = Arrays.stream(boxed).mapToInt(Integer::intValue).toArray();

// Doubles:
double[] primitives = {1.0, 2.0, 3.0};
Double[] boxed = Arrays.stream(primitives).boxed().toArray(Double[]::new);
double[] unboxed = Arrays.stream(boxed).mapToDouble(Double::doubleValue).toArray();

Note that this requires Java 8 (Stream Documentation).

For anyone unaware of what the :: means, it's used to make a Method Reference.

Allison
  • 1,925
  • 1
  • 18
  • 25
  • What about `byte` and `Byte`? It seems there is no `mapToByte`. – Anakhand Feb 08 '21 at 16:35
  • @Anakhand I haven't been doing Java dev anymore for the past few years, but you might find this helpful: https://stackoverflow.com/questions/32459683/in-java-8-is-there-a-bytestream-class – Allison Feb 09 '21 at 17:37
12

Using Guava (Ints.asList(int...) and Ints.toArray(Collection<Integer>)):

int[] intArray = ...
List<Integer> intArrayAsList = Ints.asList(intArray);
int[] intArray2 = Ints.toArray(intArrayAsList);

Note that like Arrays.asList, Ints.asList returns a list that is a view of the given array. You can't add to or remove from it, but you can set the value at a specific index. You can also copy it to a new ArrayList if you want:

List<Integer> arrayList = Lists.newArrayList(Ints.asList(intArray));

Guava has the same methods for all primitive types.

Pang
  • 9,564
  • 146
  • 81
  • 122
ColinD
  • 108,630
  • 30
  • 201
  • 202
4

If you create a utility method, that would be a one call conversion. Perhaps, in your case, that would be better than adding a whole library (of which you might just use one function), although that should be too much burden for your app.

Here's the source code for the h3xStream answer (glad it uses Apache license) ;)

Pang
  • 9,564
  • 146
  • 81
  • 122
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
1

I managed to find a solution in Java 8 :

import java.util.Arrays;
...
double[] columnsValue = {...};
Double[] values = Arrays.stream(columnsValue).boxed().toArray(Double[]::new);

I can't explain what Double[]::new means exactly, because I didn't know this feature of Java 8 before (in fact, IntelliJ IDEA wrote this for me), but it looks like it is doing new Double(double) on every element of the array.

Please note : I tested this only on desktop, I don't know if it works in Android.

Hamza Abbad
  • 564
  • 3
  • 15
  • What you're doing there is actually only *boxing* (converting a primitive to its corresponding wrapper class), not *unboxing* (converting from a wrapper class to its corresponding primitive). – Allison Jan 17 '18 at 09:08