6

I want to convert a primitive long array like long[] l1 = { 1, 2, 3, 4 }; to String. This String should have each of these values, separated by a , as delimiter.

I want to achieve this by using Java 8's streams. I already got this to work for a Long[] array, but for some reason I can't get this to work for the primitive long[].

public static void main(String[] args) {
    long[] l1 = { 1, 2, 3, 4 };
    Long[] l2 = new Long[4];
    l2[0] = new Long(1);
    l2[1] = new Long(2);
    l2[2] = new Long(3);
    l2[3] = new Long(4);
    System.out.println(longToString(l2));
}

public static String longToString(Long... l) {
    String[] x = Arrays.stream(l).map(String::valueOf).toArray(String[]::new);
    String y = Arrays.stream(x).collect(Collectors.joining(","));
    return y;
}

How can I make this work for longToString(long... l) instead of longToString(Long... l)?

Also: Did I manage to find the optimal way to convert that, or could this be simplified further? I'm pretty new to Java 8's stream features.

Bohuslav Burghardt
  • 33,626
  • 7
  • 114
  • 109
pimeys
  • 199
  • 1
  • 2
  • 10
  • 1
    It doesn't look simpler than `Arrays.toString(l)` ... – Nir Alfasi Oct 10 '15 at 22:23
  • 2
    But `Arrays.toString(l)` returns format like `[1, 2, 3, 4]`. Notice the brackets and spaces after commas. OP might have a requirement where the String should really look like `1,2,3,4`, in which case `Arrays.toString` wouldn't be suitable. If the format doesn't matter then I agree that it would be a better solution than attempting to reinvent the wheel with streams :) – Bohuslav Burghardt Oct 10 '15 at 23:00

2 Answers2

7

One way to get this working for primitive array is:

long[] arr = { 1, 2, 3, 4 };
Arrays.stream(arr).mapToObj(l -> ((Long) l).toString()).collect(Collectors.joining(","))

As explained in this answer, Arrays.stream(long[]) creates LongStream, not Stream<Long>, so you need to use mapToObj() method, which will produce Stream<String> in this case.

You can make this version even more compact by using String.valueOf() instead of Long.toString() as Tagir mentions in his comment. Because String.valueOf is overloaded for many different types, you won't have to cast the object to Long and just use method reference to String.valueOf like this:

Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(","))

Other option is to call boxed() on the result of Arrays.stream(long[]) to get Stream<Long> on which you can then call map():

Arrays.stream(arr).boxed().map(String::valueOf).collect(Collectors.joining(","))
Community
  • 1
  • 1
Bohuslav Burghardt
  • 33,626
  • 7
  • 114
  • 109
0

You could use a helper method to convert a long[] array to a Long[] array.

public static String longToString(long[] primitiveArray) {
    Long[] boxedArray = Arrays.stream(primitiveArray).boxed().toArray(size -> new Long[size]);
    return longToString(boxedArray);
}

If your goal is not doing it on your own but just pretty printing an array better use Arrays.toString as @alfasin has already mentioned.

Community
  • 1
  • 1
Marcin Król
  • 1,555
  • 2
  • 16
  • 31