简体   繁体   English

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

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

Which is the best method to convert Currently I am using something like below 哪个是转换的最佳方法目前我正在使用类似下面的内容

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

Then each object inside integers needs to be typecast to Integer. 然后整数内的每个对象都需要对Integer进行类型转换。 Is there any other way in which I can achieve this? 有没有其他方法可以实现这一目标?

With the standard JDK, here's how to do it 使用标准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());
}

If you're sure, you don't have any null values in bytes : 如果您确定,则没有任何以bytes null值:

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

If Google's Guava available in your project: 如果您的项目中有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.

相关问题 将字节数组转换为IntStream的最佳方法是什么? - What is the best way to convert a byte array to an IntStream? Java:转换ArrayList的最佳方法是什么 <Byte> 到byte []? - Java: what is the best way to convert ArrayList<Byte> to byte[]? 将原始向量转换为安全列表类型的最佳方法是什么(Arraylist) - What is the best way to convert a raw vector to type safe list(Arraylist) 转换清单 <Object[]> 进入HashMap <Integer,List<String> &gt;什么是最佳方法? - Convert List<Object[]> into HashMap<Integer,List<String>> what can be a optimal way? 转换清单 <Byte> 串 - Convert List<Byte> to string 将 RGB 字节 [][] 图像转换为字节 [] 的最佳方法 - Best way to convert RGB byte[][] image to byte[] 在Java中以美分(整数)转换美元(大十进制)的最佳方法是什么? - What is the best way to convert Dollars (Big Decimal) in Cents (Integer) in java? 将拆分器转换为 Java 中的列表的最佳习语是什么? - What is the best idiom to convert a spliterator to a list in Java? 从List中删除对象的最佳方法是什么 - What is the best way to remove objects from a List 在RuntimeExceptions上使用列表的最佳方法是什么? - What are the best way to use a List on RuntimeExceptions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM