Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an array of long to ArrayList<Long>

This sadly doesn't work:

long[] longs = new long[]{1L};
ArrayList<Long> longArray = new ArrayList<Long>(longs);

Is there a nicer way except adding them manually?

like image 421
ripper234 Avatar asked Sep 06 '25 03:09

ripper234


2 Answers

Using ArrayUtils from apache commons-lang

long[] longs = new long[]{1L};
Long[] longObjects = ArrayUtils.toObject(longs);
List<Long> longList = java.util.Arrays.asList(longObjects);
like image 191
Bozho Avatar answered Sep 07 '25 20:09

Bozho


Since others have suggested external libraries, here's the Google Guava libraries way:

long[] longs = {1L, 2L, 3L};
List<Long> longList = com.google.common.primitives.Longs.asList(longs);

Relevant javadoc for Longs.

like image 35
Esko Avatar answered Sep 07 '25 22:09

Esko