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.