This may be a bit of an easy, headdesk sort of question, but my first attempt surprisingly completely failed to work. I wanted to take an array of primitive longs and turn it into a list, which I attempted to do like this:
long[] input = someAPI.getSomeLongs();
List<Long> inputAsList = Arrays.asList(input); //Total failure to even compile!
What's the right way to do this?
Another way with Java 8.
hallidave and jpalecek have the right idea—iterating over an array—but they don't take advantage of a feature provided by
ArrayList
: since the size of the list is known in this case, you should specify it when you create theArrayList
.This way, no unnecessary arrays are created only to be discarded by the
ArrayList
because they turn out to be too short, and no empty "slots" are wasted becauseArrayList
overestimated its space requirements. Of course, if you continue to add elements to the list, a new backing array will be needed.The question asked about how to turn an array into a list. Most answers so far showed how to create a new list with the same contents as the array, or referred to third-party libraries. However, there are simple, built-in options for this sort of conversion. Some of them have already been sketched in other answers (e.g. this one). But I'd like to point out and elaborate certain degrees of freedom for the implementation here, and show the potential benefits, drawbacks and caveats.
There are at least two important distinctions to be made:
The options will be summarized here quickly, and a complete example program is shown at the bottom of this answer.
Creating a new list versus creating a view on the array
When the result should be a new list, then one of the approaches from the other answers may be used:
But one should consider the drawbacks of doing this: An array with 1000000
long
values will occupy roughly 8 Megabytes of memory. The new list will also occupy roughly 8 Megabytes. And of course, the full array has to be traversed while creating this list. In many cases, creating a new list is simply not necessary. Instead, it is sufficient to create a view on the array:(See the example at the bottom for an implementation of the
toList
method)The implication of having a view on the array are that changes in the array will be visible in the list:
Fortunately, creating a copy (that is, a new list that is not affected by modifications in the array) from the view is trivial:
Now, this is a true copy, equivalent to what is achieved with the stream-based solution that was shown above.
Creating a modifiable view or an unmodifiable view
In many cases, it will be sufficient when the list is read-only. The contents of the resulting list will often not be modified, but only passed to downstream processing that only reads the list.
Allowing for modifications of the list raises some questions:
It is possible to create a list view on the array that is modifiable. This means that changes in the list, like setting a new value at a certain index, will be visible in the array.
But it is not possible to create a list view that is structurally modifiable. This means that it is not possible to do operations that affect the size of the list. This is simply because the size of the underlying array cannot be changed.
The following is a MCVE showing the different implementation options, and the possible ways of using the resulting lists:
The output of the example is shown here:
I'm writing a small library for these problems:
In the case you care check it here.
Combining Pavel and Tom's answers we get this
A bit more verbose, but this works:
In your example it appears that Arrays.asList() is interpreting the input as list of long[] arrays instead of a list of Longs. A bit surprising, for sure. Autoboxing just doesn't work the way you want it to in this case.