繁体   English   中英

转换List的最佳方法是什么? <Byte> 列表 <Integer>

[英]What is the Best way to convert List<Byte> to List<Integer>

哪个是转换的最佳方法目前我正在使用类似下面的内容

List<Byte> bytes = new ArrayList<Byte>();
List<Object> integers = Arrays.asList(bytes.toArray());

然后整数内的每个对象都需要对Integer进行类型转换。 有没有其他方法可以实现这一目标?

使用标准JDK,这是如何做到的

List<Byte> bytes = new ArrayList<Byte>();
// [...] Fill the bytes list somehow

List<Integer> integers = new ArrayList<Integer>();
for (Byte b : bytes) {
  integers.add(b == null ? null : b.intValue());
}

如果您确定,则没有任何以bytes null值:

for (byte b : bytes) {
  integers.add((int) b);
}

如果您的项目中有Google的Guava:

// assume listofBytes is of type List<Byte>
List<Integer> listOfIntegers = Ints.asList(Ints.toArray(listOfBytes));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM