28

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?

ripper234
  • 222,824
  • 274
  • 634
  • 905
  • 5
    Related (closely) to http://stackoverflow.com/questions/754294/convert-an-array-of-primitive-longs-into-a-list-of-longs/1974363#1974363 – Eran Medan Dec 30 '09 at 11:58

7 Answers7

47

Using ArrayUtils from apache commons-lang

long[] longs = new long[]{1L};
Long[] longObjects = ArrayUtils.toObject(longs);
List<Long> longList = java.util.Arrays.asList(longObjects);
Fernando Leal
  • 9,875
  • 4
  • 24
  • 46
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
11

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.

Esko
  • 29,022
  • 11
  • 55
  • 82
8

You can avoid the copy by implementing an AbstractList via a static factory. All changes to the list write through to the array and vice-versa.

Create this method somewhere.

public static List<Long> asList(final long[] l) {
    return new AbstractList<Long>() {
        public Long get(int i) {return l[i];}
        // throws NPE if val == null
        public Long set(int i, Long val) {
            Long oldVal = l[i];
            l[i] = val;
            return oldVal;
        }
        public int size() { return l.length;}
    };
}

Then just invoke this method to create the array. You will need to use the interface List and not the implementation ArrayList in your declaration.

long[] longs = new long[]{1L, 2L, 3L};
List<Long> longArray = asList(longs);

I picked up this technique from the language guide.

Billy Bob Bain
  • 2,894
  • 18
  • 13
5

Note use of java.lang.Long, not long

final Long[] longs = new Long[]{1L};
final List<Long> longArray = Arrays.asList(longs);

Doesn't add any thirdparty dependencies.

Paul McKenzie
  • 19,646
  • 25
  • 76
  • 120
  • If you can change the problem, you can use Arrays.asList() directly and don't need that extra ArrayList construction. – Billy Bob Bain Dec 30 '09 at 13:57
  • Surely we all want to change the problem to soemthing easier to solve? So I edited my post to refelect Wayne's cleaner solution. Must remember to read though own posts first. – Paul McKenzie Dec 30 '09 at 14:15
2

Bozho's answer is good, but I dislike copying the array twice. I ended up rolling my own utility method for this:

public static ArrayList<Long> convertArray(long[] array) {
  ArrayList<Long> result = new ArrayList<Long>(array.length);
  for (long item : array)
    result.add(item);
  return result;
}
ripper234
  • 222,824
  • 274
  • 634
  • 905
  • 2
    Note that in bozho's answer, the array is not copied twice, but only once : Arrays.asList doesn't return a copy, but returns a wrapper around the array (the wrapper implements the List interface). The first copy is needed because Arrays.asList doesn't work with primitive types, hence the first conversion from primitive (long) to object type (Long). See Wayne Young's answer for the direct wrapper solution (no copy at all). – barjak Dec 30 '09 at 21:22
  • Well, but at least this is the only answer given, which actually returns an ArrayList instead of a List... – Thomas Hilger Nov 12 '21 at 08:08
2

In JDK 1.8,with Lambda and Stream API,We can do it like this:

long[] longArr = {3L, 4L, 5L, 6L, 7L};
List<Long> longBoxArr = Arrays.stream(longArr).boxed().collect(Collectors.toList());
Coder_Roc
  • 21
  • 3
-1

using Dollar you can do this conversion in only 1 line of code:

long[] longs = new long[]{1L};
List<Long> longList = $(longs).toList();

you can also box them easily:

Long[] arrayOfLongs = $(longs).toArray();
dfa
  • 114,442
  • 31
  • 189
  • 228