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?
You can use transmorph :
It also works if source is an array of ints for example.
As another possibility, the Guava library provides this as
Longs.asList()
, with similar utility classes for the other primitive types.I found it convenient to do using apache commons lang ArrayUtils (JavaDoc, Maven dependency)
it also has the reverse API
EDIT: updated to provide a complete conversion to a list as suggested by comments and other fixes.
Another way with Java 8.
I know this question is old enough, but... you can also write your own conversion method:
After you include it using static import, possible usages could be:
or