将原始 long 数组转换为 Long 列表 [英] Convert an array of primitive longs into a List of Longs

查看:30
本文介绍了将原始 long 数组转换为 Long 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的前台类型的问题,但令人惊讶的是,我的第一次尝试完全失败了.我想获取一个原始 long 数组并将其转换为一个列表,我尝试这样做:

This may be a bit of an easy, headdesk sort of question, but my first attempt surprisingly completely failed to work. I wanted to take an array of primitive longs and turn it into a list, which I attempted to do like this:

long[] input = someAPI.getSomeLongs();
List<Long> inputAsList = Arrays.asList(input); //Total failure to even compile!

这样做的正确方法是什么?

What's the right way to do this?

推荐答案

我发现使用 apache commons lang ArrayUtils (JavaDoc, Maven 依赖)

I found it convenient to do using apache commons lang ArrayUtils (JavaDoc, Maven dependency)

import org.apache.commons.lang3.ArrayUtils;
...
long[] input = someAPI.getSomeLongs();
Long[] inputBoxed = ArrayUtils.toObject(input);
List<Long> inputAsList = Arrays.asList(inputBoxed);

它也有反向 API

long[] backToPrimitive = ArrayUtils.toPrimitive(objectArray);

编辑:按照评论和其他修复的建议进行更新以提供到列表的完整转换.

updated to provide a complete conversion to a list as suggested by comments and other fixes.

这篇关于将原始 long 数组转换为 Long 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆