Is there any utility method to convert a list of Numerical types to array of primitive type? In other words I am looking for a better solution than this.
是否有任何实用方法可以将数值类型的列表转换为原始类型的数组?换句话说,我正在寻找一个比这个更好的解决方案。
private long[] toArray(List<Long> values) {
long[] result = new long[values.size()];
int i = 0;
for (Long l : values)
result[i++] = l;
return result;
}
4 个解决方案
#1
23
Since Java 8, you can do the following:
从Java 8开始,您可以执行以下操作:
long[] result = values.stream().mapToLong(l -> l).toArray();
What's happening here?
这里发生了什么?
- We convert the
List<Long>
into aStream<Long>
. -
我们将列表
转换为一条流 。 - We call
mapToLong
on it to get aLongStream
- The argument to
mapToLong
is aToLongFunction
, which has along
as the result type. - mapToLong的参数是一个ToLongFunction,它有一个很长的结果类型。
- Because Java automatically unboxes a
Long
to along
, writingl -> l
as the lambda expression works. TheLong
is converted to along
there. We could also be more explicit and useLong::longValue
instead. - 因为Java会自动打开一个长到很长的文件,在lambda表达式中使用l -> l。长时间在那里被转换了很久。我们也可以更加明确和使用Long::longValue代替。
- The argument to
- 我们调用mapToLong来得到一个长流,而mapToLong的参数是一个ToLongFunction,它有一个很长的结果类型。因为Java会自动打开一个长到很长的文件,在lambda表达式中使用l -> l。长时间在那里被转换了很久。我们也可以更加明确和使用Long::longValue代替。
- We call
toArray
, which returns along[]
- 我们调用toArray,它返回一个长[]
#2
22
Google Guava : Longs.toArray(Collection)
谷歌番石榴:Longs.toArray(集合)
long[] result = Longs.toArray(values);
#3
7
Use ArrayUtils.toPrimitive(Long[] array)
from Apache Commons.
使用ArrayUtils。来自Apache Commons的toPrimitive(长[]数组)。
Long[] l = values.toArray(new Long[values.size()]);
long[] l = ArrayUtils.toPrimitive(l);
#4
1
I don't recall about some native method that will do that but what is wrong with creating a self one ;-).
我不记得有什么本地方法可以做到这一点,但创建一个自我是错误的;-)。
public class YasinUtilities {
public static long[] toArray(Iterator<Long) values) { //Better choice would be Enumerator but easier is this way.
if(value == null) {
//return null or throw exception
}
long[] result = new long[values.size()];
Long current = null;
int i = 0;
while(values.hasNext()) {
current = values.next();
if(current == null) {
result[i++] = 0L; //or -1;
} else {
result[i++] = current.longValue();
}
}
}
}
#1
23
Since Java 8, you can do the following:
从Java 8开始,您可以执行以下操作:
long[] result = values.stream().mapToLong(l -> l).toArray();
What's happening here?
这里发生了什么?
- We convert the
List<Long>
into aStream<Long>
. -
我们将列表
转换为一条流 。 - We call
mapToLong
on it to get aLongStream
- The argument to
mapToLong
is aToLongFunction
, which has along
as the result type. - mapToLong的参数是一个ToLongFunction,它有一个很长的结果类型。
- Because Java automatically unboxes a
Long
to along
, writingl -> l
as the lambda expression works. TheLong
is converted to along
there. We could also be more explicit and useLong::longValue
instead. - 因为Java会自动打开一个长到很长的文件,在lambda表达式中使用l -> l。长时间在那里被转换了很久。我们也可以更加明确和使用Long::longValue代替。
- The argument to
- 我们调用mapToLong来得到一个长流,而mapToLong的参数是一个ToLongFunction,它有一个很长的结果类型。因为Java会自动打开一个长到很长的文件,在lambda表达式中使用l -> l。长时间在那里被转换了很久。我们也可以更加明确和使用Long::longValue代替。
- We call
toArray
, which returns along[]
- 我们调用toArray,它返回一个长[]
#2
22
Google Guava : Longs.toArray(Collection)
谷歌番石榴:Longs.toArray(集合)
long[] result = Longs.toArray(values);
#3
7
Use ArrayUtils.toPrimitive(Long[] array)
from Apache Commons.
使用ArrayUtils。来自Apache Commons的toPrimitive(长[]数组)。
Long[] l = values.toArray(new Long[values.size()]);
long[] l = ArrayUtils.toPrimitive(l);
#4
1
I don't recall about some native method that will do that but what is wrong with creating a self one ;-).
我不记得有什么本地方法可以做到这一点,但创建一个自我是错误的;-)。
public class YasinUtilities {
public static long[] toArray(Iterator<Long) values) { //Better choice would be Enumerator but easier is this way.
if(value == null) {
//return null or throw exception
}
long[] result = new long[values.size()];
Long current = null;
int i = 0;
while(values.hasNext()) {
current = values.next();
if(current == null) {
result[i++] = 0L; //or -1;
} else {
result[i++] = current.longValue();
}
}
}
}