簡體   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