5

Does anybody know an efficient, quick and clean way to convert Long[] to long[] and vice versa in Java outside the obvious for-loop with (un)box&copy?

Note: This is not about unboxing Long or boxing long. I am explicitly talking about arrays!

"There is no better way than loop and copy" would also be an answer ;)

EDIT: I am aware of the implications of doing the above and that it is better to avoid it. Just assume, I cannot avoid it and want to do it as smartly as possible without using 3rd party stuff.

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • I would go for loop and copy really. – OmniOwl Dec 13 '12 at 12:31
  • 4
    "There is no better way than loop and copy" :) - Java is pretty rigid with its array types. 'Cast-style' conversions don't work and auto-unboxing does not work for arrays (yet?). – JimmyB Dec 13 '12 at 12:32
  • 1
    An array of `Long` objects is completely different from an array of `long` primitives. There is no trick to convert them that's more efficient than looping and unboxing. – Jesper Dec 13 '12 at 12:35

8 Answers8

10

If you use apache commons lang you can do:

long[] primitive = ArrayUtils.toPrimitive(new Long[]{1L, 2L, 3L, 4L});
jakber
  • 3,549
  • 20
  • 20
Polyana Fontes
  • 3,156
  • 1
  • 27
  • 41
8

There is no better way than loop and copy

The best solution is to avoid converting in the first place. If you are worries about efficiency avoid using Long[] as it is many times larger than long[] If you need to use a collection you can try TLongArrayList which wraps a long[].

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Prerequsite for asking the question in the first place was that I cannot avoid convertion for reasons that are out of my jurisdiction. I just thought that if I cannot avoid it, I at least want to do it as smart as possible. – Fildor Dec 13 '12 at 12:48
4

When Java boxes/unboxes, it actually DOES something, even if it doesn't show it to the user.

Long[] and long[] are very different objects. So the answer is, no, you have to loop and copy. But a better question is, why do you need this? You can use them interchangeably in almost any situation... You must decide at implementation what's more important to you, object creation, or memory usage. Hopefully you use Long.valueOf to create your Longs (or autoboxing) to use Java's caching for Long values.

This is the source of apache common lang's ArrayUtils.toPrimitive. It uses a loop.

public static long[] toPrimitive(Long[] array) {
  if (array == null) {
    return null;
  } else if (array.length == 0) {
    return EMPTY_LONG_ARRAY;
  }
  final long[] result = new long[array.length];
  for (int i = 0; i < array.length; i++) {
    result[i] = array[i].longValue();
  }
  return result;
}
durron597
  • 31,968
  • 17
  • 99
  • 158
1

Java by itself does not provide any way to convert an array of objects to array of premitives.

Swagatika
  • 3,376
  • 6
  • 30
  • 39
1

ArrayUtil ReferenceHope this may help.

long[] longs = new long[]{1L};
Long[] longObjects = ArrayUtils.toObject(longs);
List<Long> longList = java.util.Arrays.asList(longObjects);
Sajid Hussain
  • 400
  • 3
  • 10
1

There is no better way than loop and copy, wrapped in a helper function that you can use over and over again.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

You can do it with ArrayUtils from apache as detailed here:

Convert an array of primitive longs into a List of Longs

Community
  • 1
  • 1
nwaltham
  • 2,067
  • 1
  • 22
  • 40
0

I can see no one answered for vice versa i.e long[] to Long[], here is the simple technique, none other than looping !

 int index=0;
 long prim[]={2,100,12,1};
 Long obj[]=new Long[prim.length];
 for(long x:prim){
     res[index++]=x;
 }