4

Java 中将列表转换为Long[]数组两种方法

 9 months ago
source link: https://www.jdon.com/67863.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Java 中将列表转换为Long[]数组两种方法

23-08-16 banq

1、使用List.toArray()方法:

List<Long> list = Arrays.asList(1L, 2L, 3L, 4L, 5L);
Long[] array = new Long[list.size()];
array = list.toArray(array);

2、mapToLong()方法

List<Long> list = Arrays.asList(1L, 2L, 3L, 4L, 5L);
long[] array = list.stream().mapToLong(l -> l).toArray();

或:

List<Long> list = Arrays.asList(1L, 2L, 3L, 4L, 5L);
long[] array = list.stream().mapToLong(Long::longValue).toArray();

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK